my_var='a'
printf '%d\n' "'$my_var"
97
If the leading character of a numeric argument is " or ' then its value is the numeric value of the immediately following character.
Put command results into an array
array=($(command))
Send mail with an attachment using mutt
cat "$message_body" | mutt -s $subject" -a "attachment_file" "$recipients"
tr -c '[:alnum:]' '[\n*]' |tr [:upper:] [:lower:]| sort -iu | grep -v '^[0-9]*$'
#!/bin/bash
# From the Advanced Bash Scripting Guide
# Another version of the "column totaler" script
#+ that adds up a specified column (of numbers) in the target file.
# This one uses indirect references.
ARGS=2
E_WRONGARGS=65
if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
then
echo "Usage: `basename $0` filename column-number"
exit $E_WRONGARGS
fi
filename=$1
column_number=$2
#===== Same as original script, up to this point =====#
# A multi-line awk script is invoked by awk ' ..... '
# Begin awk script.
# ------------------------------------------------
awk "
{ total += \$${column_number} # indirect reference
}
END {
print total
}
" "$filename"
# ------------------------------------------------
# End awk script.
# Indirect variable reference avoids the hassles
#+ of referencing a shell variable within the embedded awk script.
# Thanks, Stephane Chazelas.
exit 0
Create a variable and assign a value
var='color' val='red' eval $var=$val echo $color --> red
#!/bin/bash - # NOTE: #!/bin/sh - won't work here
filename='somerc'
while read var val; do
[ -z $var ] && continue # Ignore blank lines
[ ${var:0:1} = "#" ] && continue # Ignore comment lines
eval $var=$val
echo $var is ${!var}
done < <(cut -d' ' -f1-2 $filename)
...
: <<'COMMENTBLOCK'
echo "this line won't echo"
*()^%%)(__+% # This won't produce an error
$(rm -rf /tmp/foo) # This won't execute
${var1?} # This won't execute
COMMENTBLOCK
...
Assign variable names to list items
foo=(item1 item2 item3)
var1=${foo[0]}
var2=${foo[1]}
var3=${foo[2]}
Print a list on a line with a separator
for x in item1 item2 item3; do
list=${list:+$list,}$x
done
echo $list
item1,item2,item3
foo="Now is the time"
bar=($foo)
echo ${bar[3]} --> time
foo="The quick brown fox" for i in $(echo $foo) do echo $i done
foo=(aaa bbb ccc)
for i in $(seq 0 $(( ${#foo[@]}-1)))
do
echo ${foo[$i]}
done
NOTE: There are two constructs here that may be unfamiliar:
The $( command ) for retreiving the results of a command and
the $(( arithmetic_expression )) for doing arithmetic.
An older form of $( ... ) for command substitution is back-ticks ` ... ` but it is depreciated.
An older form of $(( ... )) for doing arithmetic is $[ ... ] but it is depreciated
echo $[ ( $RANDOM % 11 ) + 1 ] # random integer between 1 and 11 inclusive
Calculate seconds between two dates
DATE1=1941/06/28 # Some date in the past at midnight DATE2=$(date +%m/%d) # Midnight this morning TIME1=$(date --date="$DATE1" +%s) # Epoch seconds for DATE1 TIME2=$(date --date="$DATE2" +%s) # Epoch seconds for DATE2 ELTIME=$[ $TIME2 - $TIME1 ] echo $ELTIME
/usr/bin/stat -c %s $file
1843
![]() |
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 April 18, 2010 |