Bash notes

From Noah.org
Revision as of 20:09, 23 January 2008 by Root (talk | contribs)
Jump to navigationJump to search


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