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]

	for pattern matches, use
	$ [[ var == pattern ]]

        for regex matches, use
	[[ var =~ pattern ]] with ${BASH_REMATCH[n]}

	$ 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"

	[[ ${myfilename} == *.jpg ]]	# file ends with .jpg
	[[ ${myfilename} == *.??? ]]	# file ends with dot and 3 chars
        [[ ${myfilename} == *.[abc]de   # file ends with .ade, .bde, or .cde

	[[ ${myfilename} =~ [[:alnum:]]* ]]	# filename is alpha-numeric

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'

You want to invoke a program in a terminal using Gnome menu and you want the terminal to stay open after the command completes.

Use a shell script to invoke the program and put a read -n1 statement in the script after calling the application. Pressing any key will then close Gnome Terminal

For example, to build a date to Unix timestamp converter, create a menu item that uses gnome-terminal and have it call date2unix

$ cat date2unix
#!/bin/sh -
zenity --title=Date2Unix --text="Select date to convert to Unix timestamp" --date-format=%s --calendar
read -n1
exit 0
$

You want to invoke a program/command using gnome-terminal and you want the terminal to stay open after the program/command terminates.

Build a shell script shell.sh

$ cat shell.sh
#!/bin/sh -
$@
/bin/bash
exit 0
$

Now invoke the script

$ gnome-terminal -x shell.sh pwd

The terminal will stay open until you exit the shell.


Send mail to the Webmaster

logo This site best viewed with a browser
Warning: This is a Debian centric site and MAY contain peanuts.
Many thanks to Debra Lynn and Ian Murdock for making Debian possible
First created Dec 14, 2008 ~ Last revised September 21, 2011

Valid XHTML 1.0 Strict Valid CSS!