Tag Archives: bash

How to read command line parameters in BASH Scripts

Shell Scripts (BASH Scripts) can access command line parameters using the the variables $1, $2, $2 and so forth, up to $9. In fact, more parameters can be accessed by using curly brackets, like ${10}, ${187} and so forth.

Here’s an example:

If we run the script with like this

it will tell us the statement is true. Otherwise, it’ll tell us the opposite.

Note the whitespace around the evaluation: [[ ]] is actually a command (much like the == operator) and therefore needs to be surrounded with whitespace.

How to print the current date and time in BASH shell scripts

Sometimes it’s useful to print the current time and date in a BASH script. We can make use of the date command for that. By default, and if called without any parameters, it’ll print something like this:

We can shorten this to just the date by using a formatting shortcut like this:

or just the time using this format:

Formatting shortcuts can also be used together, like so:

For a complete list of shortcuts, try “man date” from the command line.

  • https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

How to use functions in a BASH shell script

BASH can deal with simple functions, and they are defined like this:

As far as I know, BASH functions cannot take or return parameters.

How to use variables in a BASH shell script

Here’s how to use simple variables in BASH shell scripts. It appears there are no data types, and everything’s a string (correct me if I’m wrong). We can define a variable by first setting it to a value, then later refer to that value with a dollar sign in front of the variable name.

Here’s an example:

Note that there are no spaces between the variable name, the equal sign or the value. Adding those will result in a runtime error.

Variables can be defined in upper or lower case letters, or a combination thereof.

BASH Variables have a global scope, unless they are prefaced with the local keyword inside functions (in which case, only said function will have access to its value).

  • http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html

How to specify FTP credentials in command line scripts

It’s easy to establish an FTP connection using the ftp command from the Linux Command Line. Sadly this command does not accept login credentials as parameters – which means that if we use it in a script, our script will pause and wait for us to type those credentials in manually. Not really suitable for automated backups.

Thanks to a clever mechanism called netrc we can create a file in the home directory of the user who runs the script and provide credentials there. Let me show you how this works.

First we create a file called .netrc. It’s a hidden file and it needs to reside in the home directory of the user who will connect via FTP. I’m going to use root for this:

The first line is just a comment to you can remember how to add parameters here. The second line is an example of a host you want to connect to. Add as many other servers as you like, all following the same pattern.

Be aware that you need to connect to the server as it is specified in the .netrc file. In the above example, if you would connect to domain.com instead, you would be asked for credentials as netrc cannot find a match.

The .netrc file needs to be readable only by this one user, otherwise connections may fail. We do this by changing the file permissions to 600:

That should do it! Try to connect with

and the connection will be established without the prompt for credentials.

If netrc isn’t working for you, or you choose not to use it, note that you can also provide FTP credentials with a here script. I find that approach a bit clunky, but the following link has details on how to do that:

  • http://unix.stackexchange.com/questions/114764/how-to-specify-username-and-password-in-ftp-command