Author: Tobin Harding Here we briefly outline some of the major use cases for brackets, parenthesis, and braces in BASH scripting, see bottom of page for definition of these three terms.
Double parentheses (( )) are used for arithmetic:
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))
The total number of supplied command-line arguments is hold by a in bash's internal variable $#. Consider a following example of simple bash script which will print out a total number of supplied command-line arguments to the STDOUT:
Save the above into a file called eg. arguments.sh and execute:
When using a bash shell all your entered commands are remembered by history library. The history library will keep track of every command you have entered. This is a default for most if not all Linux systems. However, the commands you enter are first temporarily stored into an internal memory and are written to your ~/.bash_history only after you properly exit your shell session.
Depending on your shell usage this may cause some undesired results. For example, if your connection to a remote host gets disconnected, your history file will not get updated and thus you will lose all you previously entered commands. Furthermore, while your commands for one session are temporarily stored within system's internal memory you would not be able to access it from another shell session.
Use the following linux command to force your shell to append every command entered during a current shell session into ~/.bash_history file:
Commands history is a great feature of the bash shell. However, there are times when it is best to disable it. One good example when you might prefer your bash shell commands history to be disabled is on the production server accessible from the external network where potential attacker might gain an access to your server and re-read your history file to search for useful commands, services in use or accidentally inserted passwords. Below you can find bunch of commands to help you disable history from being stored or how to remove all currently stored commands.
Where are history commands stored
All commands your enter on the shell are stored within your local directory into a file called .bash_history. This is a default history file defined by HISTFILE variable:
This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:
-bash: /bin/rm: Argument list too long
linux command to find your limit for maximum arguments:
Below is a simple function to check for a prime number. The function is_prime_number() returns False if the number supplied is less than 2 and if the number is equally divisible with some other number different than 1 and itself. If none of the previous conditions apply the function will return True. The below python script will let user to decide how many numbers needs to be check to see whether the number is prime number:
prime_numbers =0def is_prime_number(x):
if x >=2:
for y inrange(2,x):
ifnot ( x % y ):
returnFalsereturnTruefor i inrange(int(raw_input("How many numbers you wish to check: "))):
prime_numbers +=1print i
print"We found "+str(prime_numbers) +" prime numbers."
The following bash script can be used to fill empty cells within a CSV file. We assume that your file is evenly distributed so that it contains same number of columns in each row separated by comma. If your file is TABseparated use a following linux command to convert it to comma separated value file before you proceed.
$ cat test
1 2 4 4
2 3 3 3
$ sed 's/\t/,/g' test
$ cat test | tr '\t' ','
Below you can find a short script on how to convert a binary number to decimal using python casting. Casting is when you convert variable from one type to another. In this case we use python casting to convert string to decimal number that is integer. Therefore, when you attempt to use casting to convert variable type to integer make sure that you variable type imput is a string. Create a following file with a following content:
# Try to convert bunary to decimal
decimal_num =int(raw_input("Enter a binary number: "), 2)
# If we fail we ask again user to enter binary numberexceptValueError:
print"Your input is not a binary number! Please try again."else:
# Exit program if the conversion from binary to decimal was successfulbreak# print converted decimal numberprint decimal_num
Save your script as eg. binary2decimal.py, make it executable and execute:
Here a listed few of many ways how to extract number from a string. For all the examples below we will use sentence I am 999 years old. where the aim is to exctract nunber 999. Let's start by using tr command:
$ NUMBER=$(echo "I am 999 years old." | tr -dc '0-9')
$ echo $NUMBER
In case you wish to automate your things with your gmail email. Here is a simple script on how to access your gmail account with bash script. Before you run the script make sure that curl command is available on your system as this script depends on it. The below script is a great way to quickly check your gmail inbox with a single command. Open your favorite text edit and create a bash script file with some arbitrary file name eg. check_email.sh