Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bash (Unix shell)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Examples ==== With the {{code|:?|bash}} parameter expansion, an unset or null variable can halt a script. * ex.sh *: <syntaxhighlight lang="bash"> #!/bin/bash bar="foo is not defined" echo "${foo:?$bar}" echo this message doesn't print </syntaxhighlight> *: <syntaxhighlight lang="console"> $ ./ex.sh ./ex.sh: line 3: foo: foo is not defined </syntaxhighlight> Reliably printing the contents of an array that contains spaces and newlines first in a portable syntax, and then the same thing in Bash. Note that in Bash, the number of spaces before the newline is made clear. : <syntaxhighlight lang="console"> $ # In POSIX shell: $ array=( "a" "b" " > c " ) $ printf ',%s,\n' "${array[@]}" ,a, , b, , c, </syntaxhighlight> : <syntaxhighlight lang="bash"> # In Bash: declare -p array declare -a array=([0]="a" [1]=" b" [2]=$' \n c ') </syntaxhighlight> Printing an error message when there's a problem. * error.sh *: <syntaxhighlight lang="bash"> if ! lsblk | grep sdb then echo Error, line $LINENO fi </syntaxhighlight> *: <syntaxhighlight lang="console"> $ ./error.sh Error, line 130 </syntaxhighlight> Using [[xtrace]]. If errexit had been enabled, then {{code|echo quux|bash}} would not have been executed. * test.sh *: <syntaxhighlight lang="bash"> #!/bin/bash set -x foo=bar; echo $foo false echo quux </syntaxhighlight> *: <syntaxhighlight lang="console"> $ ./test.sh + foo=bar + echo bar bar + false + echo quux quux </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)