Difference between revisions of "Bash notes"
From Noah.org
Jump to navigationJump to searchLine 11: | Line 11: | ||
This is somewhat more clear | This is somewhat more clear | ||
<pre> | <pre> | ||
− | for filename in *.jpg ; mv $filename `basename $filename .jpg`_2.jpg; done | + | for filename in *.jpg ; do mv $filename `basename $filename .jpg`_2.jpg; done |
</pre> | </pre> | ||
Line 17: | Line 17: | ||
<pre> | <pre> | ||
− | for filename in *.jpg ; mv $filename `basename $filename .jpg`_2.jpg; done | + | for filename in *.jpg ; do mv $filename `basename $filename .jpg`_2.jpg; done |
</pre> | </pre> | ||
− | |||
== Variable Expansion and Substitution == | == Variable Expansion and Substitution == |
Revision as of 14:02, 20 February 2008
Contents
Turn off bash history for a session
set +o history
rename a group of files by extension
This is somewhat more clear
for filename in *.jpg ; do mv $filename `basename $filename .jpg`_2.jpg; done
This is more "correct" and doesn't require `basename`:
for filename in *.jpg ; do mv $filename `basename $filename .jpg`_2.jpg; done
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