{ code; } Command block
(code) Subshell
$'string' Quoting
${!var} Indirect Variable Reference
"'$var" | '"string' Char to Octal/Decimal/Hex
var1=foo
foo=bar
echo ${!var1} # --> bar
var2=${!var1}
echo $var2 # --> bar
This is a replacement for eval var2=\$$var1
var1=foo
foo=bar
eval var2=\$$var1
echo $var2 # --> bar
Syntax: set [options] [arg1 arg2 ...]
Example: set -- -1 -x
Explanation: -- turns off option processing so $1 is set to -1 and $2 is set to -x
Example: set -- *
Explanation: set the postitional parameters to the names of all the files in the current directory
Example: set --
Explanation: with no arguments, unset the positional parameters,
Words of the form $'string' are treated specially. The word expands to string,
with backslash-escaped characters replaced as specified by the ANSI C standard.
Backslash escape sequences, if present, are decoded as follows:
\a alert (bell)
\b backspace
\e an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\cx a control-x character
CR=$'\r' sets the variable CR to be a hex 0D (decimal 13)
LF=$'\n' sets the variable LF to be a hex 0A (decimal 10)
echo 'cat\ndog'
catndog
echo $'cat\ndog'
cat
dog
( execute in a subshell )
[ $a = 2 ] || (echo noa && echo xxx)
[ $a = 2 ] || { echo noa && echo xxx; }
Feed a block at the top with command output
(Beware the 2048 byte limit of temp files created by pipes)
command | while read LINE; do echo $LINE done also command | while read WORD1 WORD2 REST; do do something with $WORD1 do something with $WORD2 do something with $REST done
Feed a block at the bottom with command output
while read LINE; do echo $LINE done < <(command)
The space between < and < is required
and this will not work with #!/bin/sh. use #!/bin/bash
Feed a block at the top with file output
cat file | while read LINE; do echo $LINE done
Feed a block at the bottom with file output
while read LINE; do echo "$LINE" done < /etc/joe/joerc|grep -v "^#"|grep -v '^$'
This example code also ignores comment and blank lines
Using the line command instead of read
while LINE=$(/usr/bin/line); do echo "$LINE" done < /etc/joe/joerc|grep -v "^#"|grep -v '^$'
Using input file descriptors
exec 3<&0 # Associate stdin with fd3 exec 0> $INFILE # Redirect $INFILE to stdin while read LINE; do echo $LINE done exec 0<&3 # Restore stdin exec 3>&- # Close fd3
Using output file descriptors
exec 4<&1 # Associate stdout with fd4 exec 1> $OUTFILE # Redirect stdout to $OUTFILE while read LINE; do echo $LINE done < $INFILE exec 1<&4 # Restore stdout exec 4>&- # Close fd4
bar=3
foo=bar
echo ${!foo}
3
eval echo \$$foo
3
eval a=\$$foo
echo $a
3
a=(cat dog hat rat)
for key in ${!a[*]};do echo $key;done
0
1
2
3
for value in ${a[*]};do echo $value;done
cat
dog
hat
rat
a=("black cat" "hot dog")
for value in "${a[@]}";do echo $value;done
black cat
hot dog
for value in "${a[*]}";do echo $value;done
black cat hot dog
Command substitution allows the output of a command to
replace the command name. There are two forms:
$(command)
`command`
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.
echo -e $(cat now)
Now is the time For all good men To go to the aid Of their country.
echo -e "$(cat now)"
Now is the time
For all good men
To go to the aid
Of their country.
for LINE in "$(cat now)"; do echo -e x${LINE}x;done
xNow is the time For all good men To go to the aid Of their country.x
for LINE in $(cat now); do echo -e x${LINE}x;done
xNowx
xisx
xthex
xtimex
xForx
xallx
xgoodx
xmenx
xTox
xgox
xtox
xthex
xaidx
xOfx
xtheirx
xcountry.x
echo {a,b}{c,d} --> ac ad bc bd
echo {a..d}{e,f} --> ae af be bf ce cf de df
cat {file1,file2,file3} > combined_file # Concatenates the files file1, file2, and file3 into combined_file.
cp /usr/local/bin/file22.{txt,backup} # Copies "/usr/local/bin/file22.txt" to "/usr/local/bin/file22.backup"
![]() |
This site best viewed with a browser |
| Warning: This is a Debian centric site | |
| Many thanks to Debra and Ian Murdock for making Debian possible | |
| First created Apr 22, 2008 ~ Last revised August 27, 2010 |