bash shell tips

Bash shell

Redirect both stderr and stdout to somewhere (/dev/null perhaps?)
[command] > /dev/null 2>&1

If you hit ctrl-s in a terminal, it will not display anything you type.
To get the terminal back to normal, hit ctrl-q

command line sequence:
for i in `seq 1 10`; do echo $i; done
for i in {1..10}; do echo $i; done

to filter out text in a list:
ls * | grep -v 'ignorethis\|andthis\|thistoo'

To get color in the terminal add the following to your environment
(this was in FreeBSD)
alias ls="ls -G"
export TERM=xterm-color

To avoid having duplicates in your command history, add the following to a
bash configuration file:
export HISTIGNORE=\&

That way it will only store one copy of the command.

If you get error messages like "./script 16: unbalanced [" when you're using an
if statement in your shell script, make sure there is a space after the [
and before the ] in the conditional.

To execute multiple commands with the find -exec option, use escaped ampersands:
find . -name '*.txt' -exec ls -l {} \&\& touch {} \;

If you resize your terminal, set $COLUMNS or $LINES to avoid premature line-wrapping.

PS1="\[\033[G\]$PS1" to keep the prompt from getting confused with long commands (via)

To see all the commands being run by a script: set -x
To return to normal: set +x

To debug sed commands: echo "I am a pig" | sed 's/a pig/hungry/'

To generate a 30-character alphanumeric password: printf '%s\n' $(< /dev/urandom tr -dc A-Za-z0-9_ | head -c30)

Updated Oct 25, 2016