Some quick tips and tricks for use with BASH.
Check what variables are currently set for the shell environment by issuing the set
command. There are often a lot of variables set, so it may be worth while piping it through the less
command:
[user@host ~]$ set | less
'!'=0
'#'=0
'$'=5001
'*'=( )
-=0569BJPXgis
0=/bin/zsh
'?'=0
@=( )
ARGC=0
BUFFER=''
CDPATH=''
COLORFGBG='15;0'
COLORTERM=truecolor
COLUMNS=236
CPUTYPE=x86_64
CURSOR=''
DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus'
and so on.
This can be done directly from the command prompt - useful for when you might need to use the same information over and over again e.g. an IP address.
[user@host ~]$ TARGET="192.168.1.45"
[user@host ~]$ echo $TARGET
192.168.1.45
[user@host ~]$
or alternatively you can use:
[user@host ~]$ export TARGET="[email protected]:~/Downloads/"
These variables can then be used in commands e.g.
[user@host ~]$ ping $TARGET
PING 192.168.1.45 (192.168.1.45) 56(84) bytes of data.
64 bytes from 192.168.1.45: icmp_seq=64 time=0.286 ms
64 bytes from 192.168.1.45: icmp_seq=64 time=0.253 ms
64 bytes from 192.168.1.45: icmp_seq=64 time=0.230 ms
64 bytes from 192.168.1.45: icmp_seq=64 time=0.267 ms
[user@host ~]$ scp ${TARGET}file.txt ./
user@host's password:
file.txt 100% 3076 6.0MB/s 00:00
Variables can be unset using the
unset
command. For example:[user@host ~]$ unset $TARGET [user@host ~]$ echo $TARGET [user@host ~]$
There are various ways to expanding command line in BASH.
Like RegEx, we can use patterns in BASH to match certain parameters. Here is a list:
Pattern | Match |
---|---|
* | Any string of zero or more charaters |
? | Any single character |
[abc...] | Any one of the characters between the square brackets (also referred to as 'class') |
[!abc...] | Any one character NOT in the class |
[^abc...] | Any one character NOT in the class |
[[:alpha:]] | Any alphabetic character |
[[:lower:]] | Any lowercase character |
[[:upper:]] | Any uppercase character |
[[:alnum:]] | Any alphanumeric character |
[[:punct:]] | Any printable character which is not alphanumeric |
[[:digit:]] | Any single numeric character 0-9 |
[[:space:]] | Any white space character including tabs, newlines, carriage returns, form feeds or spaces |
Brace expansion can be used to quickly replace parts of commands
You can expand a list of items in to a bash command.
[user@host ~]$ echo {Sunday,Monday,Tuesday,Wednesday}.log
Sunday.log Monday.log Tuesday.log Wednesday.log
You can also use chained braces to create combinations.
[user@host ~]$ echo file{a,b}{1,2}.txt
filea1.txt filea2.txt fileb1.txt fileb2.txt
Or you can use nested braces.
[user@host ~]$ echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt
This will create file name ending 'a1' and 'a2' then 'b' and 'c'.
By default, the step is 1
[user@host ~]$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
[user@host ~]$ touch file{1..10}.txt
[user@host ~]$ ls
file10.txt file2.txt file4.txt file6.txt file8.txt
file1.txt file3.txt file5.txt file7.txt file9.txt
[user@host ~]$ rm file{3..8}.txt
[user@host ~]$ ls
file10.txt file1.txt file2.txt file9.txt
[user@host ~]$ touch file{1..10}.{jpeg,png,tiff}
[user@host ~]$ ls
file10.jpeg file1.tiff file3.png file5.jpeg file6.tiff file8.png
file10.png file2.jpeg file3.tiff file5.png file7.jpeg file8.tiff
file10.tiff file2.png file4.jpeg file5.tiff file7.png file9.jpeg
file1.jpeg file2.tiff file4.png file6.jpeg file7.tiff file9.png
file1.png file3.jpeg file4.tiff file6.png file8.jpeg file9.tiff
The stepping can also be changed:
[user@host test]$ echo {00..10..2}
00 02 04 06 08 10
[user@host ~]$ echo "the time is $(date +"%T")"
the time is 23:11:34
Be aware that using ' instead of " will result in the text being interepted as it is. For example
[user@host ~]$ echo 'the time is $(date +"%T")' the time is $(date +"%T")
#!/bin/bash
for file in $(ls);
do
extractPath="${file%%.*}";
unzip $file -d $extractPath ;
done
#!/bin/bash
extractHome=$(pwd)
thermalPath="tsr/hardware/PlatformData/thermaldata.zip"
for directory in $(ls -d */);
do
unzip -P YourPassWordHere "${directory}/${thermalPath}" -d "${extractHome}/${directory}"
done
count=0 # initialize count variable
while [ $count -lt 5 ]; do
filename="file_${count}.txt" # construct filename with count
wget -O "$filename" https://url/for/the/file
((count++)) # increment count variable
done