read
commandread [OPTIONS] variable_name
#Example Code Can't be Run in Browser/Jupyter
echo "Enter some text:"
read text
echo "You entered $text"
#Example Code Can't be Run in Browser/Jupyter
read -p "Enter some more text: " more_text
echo "Now you are telling me $more_text"
#Must be -sp, -ps means "s" is the argument of -p
read -sp "Enter the secret word: " secret
#Not printing characters means that we need to
#explicitly move to the next line
echo
echo "Was I supposed to keep $secret a secret?" ~
echo -n "Enter something quickly!: "
read -t5 user_input
if [[ -n $user_input ]]; then
echo "Congrats! You beat the clock"
else
echo
echo "Too Slow! Better luck next time"
fi
mapfile
command reads STDIN into an array, breaking it up at newlinesmapfile [OPTIONS] array_variable
mapfile numbers<<HERE
1
2
3
4
5
HERE
for number in ${numbers[@]}; do
echo -n "$number, "
done
echo
mapfile
command is generally more efficient, but is a recent addition to bashwhile
loopread
commandwhile read line; do
echo $line
done < data/numbers.txt
max=""
while read line; do
for word in $line; do
if [[ ${#word} -gt ${#max} ]]; then
max=$word
fi
done;
done < data/lines.txt
echo $max;
printf
command allows output to be formatted with more control than echoprintf
command after the format stringprintf "%d is a number\n" 30
printf "%10d is a number\n" 30
printf "%010d is a number\n" 30
printf "%-10d is a number\n" 30
printf "%d is a big number\n" 10000000000
printf "%'d is a big number that is easier to read" 10000000000
printf "%f is a float\n" 30
printf "%f is a float\n" 30.1345
printf "%.2f is a truncated float\n" 30.12345
printf "%'.2f is a truncated , yet big, float" 3000000000.12345
printf "%s is a string\n" "Hello there"
#All Arguments are always printed
printf "%s was passed as an argument\n" Hello there
printf "%3s doesn't truncate the string\n" "A long string"
printf "%.3s does truncate the string\n" "A long string"
printf "%10.3s truncates the string\
, but prints with a width of 10" "A long string"
printf
¶%q
will escape your string into an appropriate format for bash%(fmt)T
converts seconds into a user specified date stringfmt
is other format commands for dates, similar to strftime
function in Cprintf %q "A directoryname with spaces/"
printf "\n"
printf "%(%A the %d of %B, %Y, at %r)T\n" -1
printf "%(%A the %d of %B, %Y, at %r)T" 0
source
command.
is an alias for the source
command. lots_of_definitions
source other_definitions
more src/shell/definitions.sh
./src/shell/definitions.sh
echo $pi
. src/shell/definitions.sh
echo ${alphabet[*]}
COMMAND &
- executes command in backgroundbg JOB_SPEC
- sends command to backgroundfg JOB_SPEC
- brings background command to foregroundjobs
list all currently running processes launched from this shellps
list all processes on the computerps
Command¶ps
command by default displays the pids for processes launched from this shellps
ps -f -ubryan | more
kill
is a more general command then just ended processeskill
command can send signals to running processeskill -l
kill SIGNAL PID
# Launch a random background job
htop &
kill -15 12581
jobs
kill -9 12581
SIGHUP
which is sent when a terminal closesnohup COMMAND &
`command`
$(command)
$(command)
html_files=$(find . -name "*.ipynb")
echo $html_files
ps_out=$(ps)
echo ${ps_out::10}
nesting=$(echo $(ls))
echo $nesting
ipynb
files in the directory, with ipynb
removed${var//pattern/substitute}
var=$(ls *ipynb)
echo ${var//.ipynb/}
&&
,||
, and ;
operators are used to chain commands togethercommand1 && command2
only executes command2 upon successful exit of command1command1 || command2
only executes command2 upon unsuccessful exit of command1command1 ; command2
always executes command2rm /home 2> /dev/null || echo "You can't do that"
[[ 1 -eq 1 ]] && echo "That is true 1"
[[ 1 -eq 2 ]] && echo "That is true 2"
[[ 1 -eq 2 ]] || echo "That isn't true 2"
( COMANDS )
echo $(pwd)
(
cd ~
echo $(pwd)
)
echo $(pwd)
printf "%'d is a big number\n" 1000000
(
LANG=es_ES.UTF-8
printf "%'d is a big number\n" 1000000
)
printf "%'d is a big number\n" 1000000
wait
that will pause the execution of the script until all child processes have returned#Supress notification of completed background jobs
set +m
(
for letter in {A..M}; do
echo "$letter ";
sleep 0.5;
done;
)&
(
for number in 1 2 3 4 5 6 7; do
echo "$number ";
sleep 0.25;
done
)&
wait
echo "EVERYTHING IS AWESOME"
parallel
command executes a command in parallel given a list of arguments separated by :::
parallel echo ::: A B C ::: 1 2 3
parallel --pipe
allows parallel processing of STDINsem
command is useful to combine with backgrounded subprocesses to limit how many run at a timeparallel echo ::: A B C ::: 1 2 3
parallel jupyter-nbconvert {} --to html ::: *.ipynb
time (grep -P "\d\d\d-\d\d\d-\d\d\d\d" ~/Research/Data/wackypediaFlat.slim | wc -l)
#grep -P "\d\d\d-\d\d\d-\d\d\d\d" ~/wackypediaFlat.slim | wc -l
time parallel --pipe --block 100M 'grep -P "\d\d\d-\d\d\d-\d\d\d\d" | wc -l' < ~/Research/Data/wackypediaFlat.slim
# There are better ways to do this, ie all in one search
for letter in {A..Z}; do
(
n=$(grep -P "($letter)\1" ~/wackypediaFlat.slim | wc -l)
echo "$n double $letter's found"
sleep 0.5;
)&
done;
wait
# There are better ways to do this, ie all in one search
for letter in {A..Z}; do
(
n=$(sem --id $$ -j3 grep "${letter}${letter}" ~/wackypediaFlat.slim | wc -l)
echo "$n double $letter's found"
sleep 0.5;
)&
done;
sem --wait --id $$
parallel --pipe
split -l1 numbers.txt numbers_aa
ls x*
more numbersaa
echo $((0 + 11))
echo $((10/6))
echo $((10 * 6))
echo $((10 % 6))
x=10
((x++))
echo $((x += 1))
echo $((x += 1))
echo $((3.14 + 11 ))
bc
command is usedscale=PRECISION;
bc <<< "0+5"
bc <<< "scale=2;10/6"
bc <<< "scale=2;3.14 + 11"
bc <<< "scale=2; sqrt(9)"
echo "scale=2; c(0)" | bc -l
echo "scale=2; s(0)" | bc -l