Bash

Get help

yelp info:bash

Advanced Bash Scripting Guide

Discover what shells

cat /etc/shells

Discover which shell

echo $SHELL
ps $$
ps -p $$

Discover shell type

If the first character of $0 is a dash then the shell is a login shell. This means that bash was started because you just logged in with a user name and password from a console, you logged in via ssh, you're using X and started xterm with the -ls parameter, you started a sub-shell with the -l parameter, etc

~/profile /etc/profile ~/.bashrc /etc/bash.bashrc

The profile scripts are called by bash for login shells

The rc scripts are called by bash for non-login sub-shells.

Because environment variables get passed on to subshells they can be set in profile

To have the commands in /etc/bash.bashrc executed for login shells, source it from /etc/profile

To have the commands in ~/.bashrc executed for login shells, source it from ~/.profile

Scope

Functions in a POSIX shell have no provision for local variables. Bash does provide for local variables using:

	function foo {
	  local a b c
	  ...
	}

Test

Use either

	$ test [expression]
	or
	$ [ [expression] ]

	$ s1 = s2	# strings s1 and s2 are the same
	$ s1 != s2	# strings s1 and s2 are not the same
	$ n1 -eq n2	# integers n1 and n2 are equal
	$ n1 -ne n2	# integers n1 and n2 are not equal

Complete examples:

	if [ $a -eq $b ]; then
	  echo "equal integers\n"
	fi

	[ "$a" = "$b" ] && echo "equivalent strings\n"
	[ -f /tmp/myfile ] || echo "file not found\n"

After Hours

You have the numbers 123456789, in that order. Between each number, insert either nothing, a plus sign, or a multiplication sign, so that the resulting expression equals 2002. Write a program that prints all solutions. (There are two.)

for e in 1{,+,*}2{,+,*}3{,+,*}4{,+,*}5{,+,*}6{,+,*}7{,+,*}8{,+,*}9; do
 echo $e = $(($e))
done | grep '= 2002'

Send mail to the Webmaster

logo This site best viewed with a browser
Warning: This is a Debian centric site
Many thanks to Debra and Ian Murdock for making Debian possible
First created Dec 14, 2008 ~ Last revised March 29, 2010

Valid XHTML 1.0 Strict Valid CSS!