This was written at my very woke moments when I realize I’m probably going to forget about some tiny little details about bash shell if I don’t use all of them that much in the future. And also, sometimes, snippets can be handy.
Basics in case I have brain damage
These are some very basic stuffs, in case you really need them.
Shebang!
It’s a common thing to add headers (shebang
in nature) to shell script to designate the shell you wanna use. You can go with sh
or bash
or whatever works.
1 |
1 |
It can have parameters:
1 |
Even with PHP script, though it’s off topic.
1 |
Let there be variables
Everything is “string”.
You wanna have literals? Gotcha covered.
1 | # No space after variable name or after the equal sign. |
Pass a variable? No problem.
1 | # Yes, you gotta quote them |
You can even use an output of a command? Coooool!
1 | a="Today is $(date +%Y-%d-%m)" |
if
Well, about if
control flow, shell has quite a bit of history.
So, there are a lot of ways to write conditions, namely []
, test
command, [[]]
, let
and (())
.
Generally, it is written in this manner:
1 | if CONDITION |
As for how condition is written, that’s where the “goodies” are.
[]
or test
1 | # How spaces are put is very strict here |
[[]]
is not very universal, you could probably only use it with Bash or ksh. It was introduced into Bash at the version 2.02. But it is much more capable than []
in terms of expressiveness.
1 | # You can use wildcards here. But still, spaces are very |
(())
is basically doing arithmetic calculations, but its result can be used as a logic value for if
control flow.
1 | # In `(())`, you can omit dollar sign for variables |
while
loops
If you understand how conditions are written for if
statement, then while
should be no problem for you. There is also another thing called until
loops, which work very much like while
loops, the only difference being until
loops only run its loops when CONDITION are NOT met.
1 | while CONDITION |
for
loops
for
loops have two styles, one like C/C++, the other like python. You can also do break
and continue
by the way.
C style for
loop
1 | for ((i=1; i<=100; i++)) |
Python style for
loop
1 | # List values |
Unix special variable list
Variable | Description |
---|---|
$0 | The filename of the current script. |
$n | These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on). |
$# | The number of arguments supplied to a script. |
$* | All the arguments, all double quoted. If a script receives two arguments, $* is equivalent to $1 $2. |
[email protected] | All the arguments, all individually double quoted. If a script receives two arguments, [email protected] is equivalent to $1 $2. |
$? | The exit status of the last command executed. |
$$ | The process number of the current shell. For shell scripts, this is the process ID under which they are executing. |
$! | The process number of the last background command. |
Snippets
Get script path
1 | shell_dir=$(dirname "$0") |
Delete a directory if exits
1 | if [ -d "$dir" ] ; then |
Create a directory if it doesn’t exist
1 | if [ ! -d "$dir" ] ; then |
About this Post
This post is written by Dizy, licensed under CC BY-NC 4.0.