Multiple line commands (Bash)
(Doorverwezen vanaf Command line wrap (Bash))
Naar navigatie springen
Naar zoeken springen
There are multiple ways to split a command across multiple lines.
With backslash
Example [1]:
tar -cvpzf /share/Recovery/Snapshots/$(hostname)_$(date +%Y%m%d).tar.gz \ --exclude=/proc \ --exclude=/lost+found \ --exclude=/sys \ --exclude=/mnt \ --exclude=/media \ --exclude=/dev \ --exclude=/share/Archive \
You can align the backslashes. E.g.:
tar -cvpzf /share/Recovery/Snapshots/$(hostname)_$(date +%Y%m%d).tar.gz \ --exclude=/proc \ --exclude=/lost+found \ --exclude=/sys \ --exclude=/mnt \ --exclude=/media \ --exclude=/dev \ --exclude=/share/Archive \
With parameter array
As an example: Command wp help post meta patch update
can be rewritten as follows. Note that the opening "(" has on the first line:
params=( help post meta patch update ) echo ${params[@]} wp "${params[@]}"
or even including the command:
params=( wp help post meta patch update ) echo ${params[@]} ("${params[@]}")
Strings with long texts
How to assign a long text to a string in a practical way?
Subvariables
i0="Jantje zag eens pruimen hangen " i1="Oh als eieren zo groot. " i2="De tuinman zag zijn bolle wangen " i3="Sloeg de vuile gapper dood." i=$i0$i1$i2$i3 echo $i
Incremental variable
Shorter than above and with full control over lines:
i="Jantje zag eens pruimen hangen " i+="Oh als eieren zo groot. " i+="De tuinman zag zijn bolle wangen " i+="Sloeg de vuile gapper dood." echo $i
As a multi-line literal
i="Jantje zag eens pruimen hangen, oh als eieren zo groot. De tuinman zag zijn bolle wangen, sloeg de vuile gapper dood." echo $i
Output:
Jantje zag eens pruimen hangen, oh als eieren zo groot. De tuinman zag zijn bolle wangen, sloeg de vuile gapper dood.
- I use this approach quite often, but I somehow feel that it's messy
- Under certain conditions (e.g., writing an Apache virtual host file,
\n
is an escape for newline, but not always
Param array
i=( Jantje zag eens pruimen hangen, oh als eieren zo groot. De tuinman zag zijn bolle wangen, sloeg de vuile gapper dood. ) echo ${i[@]}
With output:
Jantje zag eens pruimen hangen, oh als appelen zo groot. De tuinman zag zijn bolle wangen, sloeg de vuile gapper dood.
- The individual lines are treated as cells in the array
- Outputting the whole array through
[@]
strings all cells together, separated by a space - It's pervious to identation - Handy!
- How to include whitespace (tabs, enter)?
Example: Multiple conditions in tests
if [[ ${site_array[$i,tag]} =~ "_bal_" ]] && \ [[ ${site_array[$i,tag]} =~ "_cb_" ]] && \ [[ ${site_array[$i,tag]} =~ "_dvb8_" ]] && \ [[ ${site_array[$i,language]} =~ "en" ]] then ... fi