The bash shell#
These are programming notes for programming in the bash shell.
bash startup#
~/.bashrc | executed when a new shell is started |
~/.profile | executed when the user logs in |
/etc/profile.d/* | executed when any user logs in |
Programs such as Netbeans are not able to see environment variables set in .bashrc as it is run after the environment Netbeans is started in has been constructed. Environment variables set in .profile can be seen by programs started after login.
You must logout and back in in order for changes to .profile or /etc/profile.d/*to take affect.
Redirecting IO#
2>file send stderr to file 2>&1 send stderr to stdout
Accessing shell arguments#
$0 command name $1 first argument $@ all arguments (except $0) $# number of arguments shift shift arguments "$@" passing all arguments to another shell or program Use with double quotes. If you do not do it this way, arguments with spaces will not be handled correctly. $? return code of last executed command
Quotes#
$ variables do not get evaluated within single quotes.
$ variables get evaluated within double quotes.
List iteration#
(In this example, through all the arguments)for var in $@; do echo $var done
for file in *.c; do echo $file done
While loop with math#
m=10 i=0 while [ $i -lt $m ]; do echo $i let i+=1 done
Case statement#
case $1 in xxx) echo 1 echo 2 ;; yyy) echo 3 echo 4 ;; *) echo Unknown argument $1 ;; esac
Variable assignment#
var=val no space around =
For loops#
for ((i=1 ; i < 5 ; i++)); do echo $i done
Arrays#
Start from 0
a[3]=val assignment ${a[3]} get value a=(val1 val2 val3) make array ${#a[@]} number of elements ${a[@]} all elements as a list
Conditionals#
if [ condition ]; then .... elif [ condition ]; then .... else .... fi
Conditions#
-f file if file exists -d dir if dir exists a = b string equal a != b string not equal -z x string is null x string is not null x -eq y x numerically equal to y x -ne y x numerically not equal to y x -gt y x greater than y x -lt y x less than y x -ge y x greater than or equal to y x -le y x less than or equal to y ! cond not condition if [ cond ] && [ cond ]; then ... fi if [ cond ] || [ cond ]; then ... fi
Here documents - inline input#
cmd << label .. .. .. labelall lines up to label are input into command
Getting user input#
echo This program ..... while true; do read -p "Is that what you with to do? " yn case $yn in [Yy]* ) break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done
Setting command line editing keyboard configuration#
set -o emacs set -o vi
Add new attachment
Only authorized users are allowed to upload new attachments.
«
This page (revision-16) was last changed on 14-Oct-2019 05:56 by BlakeMcBride
JSPWiki v2.10.4