Category ‘Bash’
- TECHPULP |
How to check if a Bash variable is set or not
There is no direct method in bash to determine if a variable is set or not. But we can use “parameter expansion” feature provided by bash. The following example script determines if bash variables MYVAR and MYVAR1 are set or... - TECHPULP |
How to generate random password using Bash shell scripting
All Linux systems have a special device “/dev/urandom” which can throw random bytes while reading. But this device gives binary data which may contain non-printable characters. However as we need human-readable characters for a password, we can pick all alpha-numeric... - TECHPULP |
How to get PID of a process by name in bash script
The following bash shell script looks at the output of “ps” command to identify the PID of a process. This script takes name of the process as command line argument. [neo@techpulp ~]# cat getpid.sh #!/bin/bash ps -e | grep "$1$"... - TECHPULP |
How execute a command in a separate directory without changing current working directory in Bash
The following example shows how to move to a directory before executing a command and then automatically return back to the old diretory. This effectively runs the command with current directory as a different directory. [neo@techpulp ~]# pwd /home/neo [neo@techpulp... - TECHPULP |
How to get the PID of current bash shell script
A special variable “$$” can be used with in a shell script to retrieve self process ID (PID). This is useful for monitoring scripts to store their PID in a file for other maintenance script to identify it. The following... - TECHPULP |
How to get exit code of previous command in Bash
The special variable “$?” returns the exit code of last executed command in Bash shell script. The following example gets the exit code of “grep” command whose exit code will be zero if finds match and 1 otherwise. #!/bin/bash grep... - TECHPULP |
Bash shell script to convert string from lower to upper case and vice versa
The “tr” command can be used to convert a string variable to upper or lower case. The following function in a bash script converts first argument to lower case. Convert the given argument into an all lower case string. toLower()... - TECHPULP |
How to find file name of the script with in a bash shell script
With in a bash shell script, you can use the very first command line argument ($0) to find the name of the script. The following example explains it. [neo@techpulp ~]# cat myscript.sh #!/bin/bash echo The script name is $0 [neo@techpulp... - TECHPULP |
Where my bash command history is stored
The bash shell stores all your command history in a file of your home directory. Typically it will be stored in ~/.bash_history. This is a global history of all bash terminals which are open. The bash shell appends all commands... - TECHPULP |
How to change my Bash shell prompt
The environment variable “PS1” determines how the shell prompt should appear in Bash. You can find the current setting of PS1 as shown below. [neo@techpulp ~]# echo $PS1 [\u@\h \W]\$ [neo@techpulp ~]# Bash supports some pre-defined automatic variable substitutions in...