Difference between revisions of "Bash notes"
From Noah.org
Jump to navigationJump to searchLine 5: | Line 5: | ||
<pre> | <pre> | ||
set +o history | set +o history | ||
+ | </pre> | ||
+ | |||
+ | == Variable Expansion and Substitution == | ||
+ | |||
+ | Bash can do some freaky things with variables. It can do lots of other substitutions. See "Parameter Expansion" in the Bash man page. | ||
+ | |||
+ | * ${foo#pattern} - deletes the shortest possible match from the left | ||
+ | * ${foo##pattern} - deletes the longest possible match from the left | ||
+ | * ${foo%pattern} - deletes the shortest possible match from the right | ||
+ | * ${foo%%pattern} - deletes the longest possible match from the right | ||
+ | * ${foo=text} - If $foo exists and is not null then return $foo. If $foo doesn't exist then create it and set value to text. | ||
+ | |||
+ | == Statements == | ||
+ | |||
+ | === Loop on filenames in a directory === | ||
+ | |||
+ | <pre> | ||
+ | for foo in *; do { | ||
+ | echo ${foo} | ||
+ | }; done | ||
+ | </pre> | ||
+ | |||
+ | === Loop on lines in a file === | ||
+ | |||
+ | <pre> | ||
+ | for foo in $(cat data_file.txt); do { | ||
+ | echo ${foo} | ||
+ | }; done | ||
</pre> | </pre> |
Revision as of 20:09, 23 January 2008
Contents
Turn off bash history for a session
set +o history
Variable Expansion and Substitution
Bash can do some freaky things with variables. It can do lots of other substitutions. See "Parameter Expansion" in the Bash man page.
- ${foo#pattern} - deletes the shortest possible match from the left
- ${foo##pattern} - deletes the longest possible match from the left
- ${foo%pattern} - deletes the shortest possible match from the right
- ${foo%%pattern} - deletes the longest possible match from the right
- ${foo=text} - If $foo exists and is not null then return $foo. If $foo doesn't exist then create it and set value to text.
Statements
Loop on filenames in a directory
for foo in *; do { echo ${foo} }; done
Loop on lines in a file
for foo in $(cat data_file.txt); do { echo ${foo} }; done