Here are some example BASH functions. For more information, see the online BASH Function documentation or the BASH man page.
Declaring Functions
A BASH function must first be declared. One of the two methods can be used to declare a BASH function:
Method #1: function name compound-command
function say_goodbye {
echo "goodbye"
}
Method #2: name() compound-command
say_hello() {
echo "hello"
}
Using Functions
Just call the name as you would any other simple command:
say_hello
hello
Passing Variables
Pass variables into the function by space delimited fields and accept parameters using $1, $2 and so on.
my_function() {
echo "$1"
}
Example:
my_function hello this is a test
hello
To expand all variable parameters inside the function, use $@
log() {
echo "[$(date)]: $@"
}
log this is a test
[Sun Jun 22 10:21:26 PDT 2008]: this is a test
Return Codes
Functions return an exit status to their caller:
subtest() {
return 1
}
The function will return 1:
subtest || echo "Returned: $?"
Returned: 1
Since the function returned 1, it failed the test of || (or).
Getting an "Argument list too long" error using Linux?
This is because you are passing too many arguments to an executable.
For example, using *.html below results in too many arguments to /bin/rm:
rm *.html
bash: /bin/rm: Argument list too long
One solution is to use Bash control loops to iterate through arguments.
for HTMLFILE in *.html
> do
> rm -f $HTMLFILE
> done
Alternatively, the find command can be used to manipulate large data sets.
The following find command is identical in result to the for loop shown above.
bash$ find ./ -maxdepth 1 -name '*.html' -exec rm -f {} \;
Have you ever seen this error when using tar?
tar -czf etc.tgz /etc
Removing leading `/' from member names
Tar is removing the leading / from the archive file, and warning you about it. Although you can redirect STDERR to /dev/null, doing so can result in missed errors. Instead, use tar with the -P or --absolute-names switch. They do the same thing: leave the leading / in the archived files.
tar -czPf etc.tgz /etc
When you untar the archive without -P, the leading / will still equate to your current working directory. Use the -P when untarring to restore from archive to the absolute path name. For example:
The following creates ./etc (dot, slash, etc)
tar -xzf etc.tgz
This overwrites /etc (slash, etc)!
tar -xzPf etc.tgz
Running into this?
bash$ sudo rpm -e zlib-devel
error: "zlib-devel" specifies multiple packages
This is because zlib-devel.i386 and zlib-devel.x86_64 are both installed. It is possible to remove them individually:
bash$ sudo rpm -e zlib-devel.i386
bash$ sudo rpm -e zlib-devel.x86_64
By default, Fedora, CentOS and RedHat shells do not specify the architecture of an RPM in the query format. This can lead to duplicate entries from queries:
bash$ rpm -q zlib-devel
zlib-devel-1.2.3-14.fc8
zlib-devel-1.2.3-14.fc8
You can use the --queryformat switch when running rpm -q, or configure the query format setting in ~/.rpmmacros.
bash$ rpm -q --queryformat "%{name}.%{arch}\n" zlib-devel
zlib-devel.i386
zlib-devel.x86_64
bash$ cat ~/.rpmmacros
%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}
bash$ rpm -q zlib-devel
zlib-devel-1.2.3-14.fc8.i386
zlib-devel-1.2.3-14.fc8.x86_64