- A good (albeit comprehensive) cheatsheet is found here: https://gist.github.com/tuxfight3r/60051ac67c5f0445efee
- You'll eventually find a subset of the shortcuts that you use most often.
- My personal list is:
- Ctrl + A / Home key: move to the beginning of the line
- Ctrl + E / End key : move to the end of the line
- Ctrl + K : delete from cursor to end of the line
- Ctrl + W : delete the current word
- Ctrl + Left/Right arrow : move cursor faster (one word at a time)
- Use the Up arrow (or Ctrl + P) to pull up previous commands
- Type
!!
and press Enter to directly execute the last command - Use Ctrl + R to search your previous commands
- Typing a few characters then pressing your keyboard's TAB key autocompletes either:
- the command (if you're typing a command)
- files in the current directory (if you're providing a file argument to a command)
- If there are multiple files that match what you've typed so far, press tab several times to see a list of options
- Several shell commands in a text file
- Typically named with
.sh
file extensions - Run scripts by either:
- Setting them to executable with
chmod +x myscript.sh
, then executing them with./myscript.sh
- Passing the script to
bash
as an argument (eg.bash myscript.sh
)
- Setting them to executable with
- Write scripts in a text editor (eg. VSCode or
nano
) - All bash scripts should start with
#!/bin/bash
on the first line to tell the computer what to use to run it
- Define variables with this notation:
- Note: no space between variable name, equal sign, and assigned value
varname="varvalue"
- Use variables with this notation:
- Note: the
$
is required when using bash variables
- Note: the
echo $varname
myfirstscript.sh
#!/bin/bash
# This is a comment
myvar="World"
echo "Hello $myvar!"
- Special variables named
$1
,$2
, etc... - Set by bash to the arguments provided to the command
- (not tested) Note on terminology:
- A "parameter" is the name of the variable in the context of the function (eg.
$1
) - An "argument" is the value provided to the function / script that gets assigned to each parameter
- Sometimes these two words are used interchangeably
- A "parameter" is the name of the variable in the context of the function (eg.
- (not tested) Note on terminology:
Suppose we have a shell script named myparamscript.sh
#!/bin/bash
echo "Hello $1!"
Then we can change what it does when we run it in the shell
$ bash myparamscript.sh World
Hello World!
$ bash myparamscript.sh Bash
Hello Bash!
$ bash myparamscript.sh Everyone
Hello Everyone!
Parameter names can be made unambiguous using curly braces. This notation is called Parameter Expansion.
#!/bin/bash
# This is a comment
myvar="World"
echo "Hello ${myvar}s!"
Parameter Expansion can also be used to substitute or set default values:
$ myvar=
$ echo ${foo:-"default value"}
default value
$ myvar="Toronto needs more trees"
$ echo ${myvar:8:5}
needs
$ echo ${i//needs/must have}
Toronto must have more trees
- Store output of a command into a variable
myvar=$(ls -1)
echo $myvar
command1 && command2
will run command1, then run command2 only if command1 was successful- eg.
prepfiles && analyzefiles
- eg.
command1 || command2
will run command1, then run command2 if command1 failed- eg.
downloadfile || printerror
- eg.
- Perform different actions based on the value of a condition
- Repeat actions while a condition is true or until a condition is true
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo $count
count=$((count +1))
mkdir "mynewdir$count"
done
echo "Finished."
#!/bin/bash
count=1
until [ $count -gt 5 ]
do
echo $count
count=$((count +1))
mkdir "mynewdir$count"
done
echo "Finished."
- Run commands within the loop for each item in a variable / returned from a command
- This script renames all .txt files in a folder to .TXT
for i in $(find *.txt)
do
echo $i
mv $i ${i//.txt/.TXT}
done