This topic was published by DevynCJohnson and viewed 678 times since "". The last page revision was "".
- Topics - 444
BASH can be a difficult shell to use and many commands may be too long to type. Some users may find some commands to hard to remember, and then other users want to customize BASH and make it more interesting. This article will help to solve some of these issues and show readers how to make BASH better.
Using Alias to Make Better Commands
Users could make an alias command. This is a command that is a shortcut to another. For illustration, many users may not like typing "sudo add-apt-repository" before every PPA that they wish to add. This problem has a fast, easy solution. The user can type
alias addppa='sudo add-apt-repository'
Alias is the command that makes aliases and "addppa" is the alias. Now, a user can type "addppa" before the PPA address that they wish to add to their repository list. The command "addppa" is easier to remember and type than "sudo add-apt-repository". The alias command says exactly what the user wants to accomplish - add a ppa.
Users may want to make BASH more interesting by making alternative ways to leave the shell. Instead of typing "exit", users can make a new command.
alias bye='exit'; goodbye='exit'; Bye='exit'
The above line will allow users to use everyday words to indicate to BASH that it needs to turn off. Users can now say "bye", "goodbye", and "Bye" (remember that BASH is case-sensitive, so "bye" and "Bye" are two different commands).
Users could make alias commands as bizarre as they wish. For illustration,
alias stay-on='exit'; pizza='exit'. Now, when a user types "stay-on" or "pizza", the terminal will turn off.
These aliases will remain valid until the terminal closes. If a user wishes to make the aliases permanent, then they need to add them to ~/.bash_aliases. Open ~/.bash_aliases in the preferred text editor, and type the alias lines into the file the same way they are written in a terminal.
For people new to BASH, they may have problems remembering that "pwd" Prints the Working Directory. The user could make an alias command "whereami" to act as pwd. Now, the user can type "whereami" and the command will print the user's location on the storage device.
Writing Minor Scripts
Users could make small scripts instead of aliases to make new commands. For example, many Launchpad users may not like typing "lsb_release -rd; apt-cache policy APPNAME" (where APPNAME is the name of the app that they are reporting) every time they report a bug. The solution is simple - make a script. (The code below is from https://launchpad.net/enhance-bash, and when this script is installed, the command is lpbug.)
#!/bin/bash # The output of this command is commonly needed when filing bug reports on Launchpad lsb_release -rd; apt-cache policy $@
The first line is the hashpling that all BASH scripts contain. The next two lines are comments. The "$@" is a variable that represents all arguments and parameters for the script. If a user types "lpbug gimp" (after the script is installed), then the output will be the same as if the user typed "lsb_release -rd; apt-cache policy gimp". The script prints the Linux distribution type and version as well as information about the program that is used as the argument - GIMP in this case.
Many of the scripts on https://launchpad.net/enhance-bash are shortcut commands or scripts that make many tasks easier and faster to perform. Looking at some of these scripts may help new BASH programmers learn how to make scripts.
Once a user has written a script, make the script's filename the same as the command to be written. This includes the file extension. For illustration, the script above is called "lpbug"; therefore, the command will be "lpbug". If a developer does not remove the file extension, then once the script is installed, the command would be "lpbug.sh". To install, the script should be copied to /usr/bin/ (using root privileges) if the user wishes to use this script like any other command. Now, the user can open a terminal and type "lpbug gimp". When making script that only root should use, place the scripts in /sbin/ (using root privileges).
Comparing Aliases and Self-made Scripts
Users may use aliases or self-made scripts to make a shortcut command, but sometimes one is better than the other. For example, the command "sudo apt-get install APP" (APP is the app to be installed) can be given an alias by placing "alias addapp='sudo apt-get install'" into the alias file (~/.bash_aliases). Alternatively, users can make a script by typing the hashpling on the first line and "sudo apt-get install" as the second. After the script is copied to /usr/bin/, the user can start using their new command - addapp. No matter which way the user made the command "addapp", the command behaves the same way. However, there are reasons for choosing one method over the other. Many (but not all) reasons and differences between the two are listed below to assist programmers in deciding which method to use. In general, scripts are better.
- When a command is made with alias, only that user can use the command. If a script is made and installed, all users can access the command.
- BASH scripts will not use system aliases, but they will use installed scripts.
- It is easier to share and distribute scripts than some plain text file that must be copied into the alias file.
- Larger sets of code should be put in scripts, not aliases. The alias command is meant for short pieces of code.
- An edited alias can be used once the BASH terminal is reset. Modified scripts can be used instantly without restarting any application.
- Alias commands are great for users who want a particular shortcut or command that they do not want others to use or others cannot use. For example, making an alias that takes a particular user to one of their personal folders is only needed by the user, not everyone. As another example, a manager may have an alias command that notifies all employees that the meeting was delayed. In this instance, only the manager needs this command. No one else should be able to execute the command.
- The user suspects that adding a script will cause system conflicts, so an alias is made instead.
Making New Commands with Other Languages
Users do not need to use BASH scripts to make new commands for the terminal. Users may program a BASH command in any language that they wish. Once the file has been written, compile the code (if the chosen language requires it). Then, place the written file in /usr/bin/ (with root privileges) if all users may use the command. Alternatively, place the file in /sbin/ if only the root is permitted to use the command.
Making Permanent Environment Variables
Environment variables are variables that any command, program, or script can access. If a variable is set in a script, only that script can use it. If a user creates a variable in a terminal, the variable can only be used in that terminal until it closes. Then, the variable is gone forever. Users can make permanent environment variables by putting them in the file ~/.bashrc. This file contains functions and variables that the shell sets up when the user opens a terminal. Because the file is in the user's home folder, only that user can access the data that is created from that file. Users may put aliases in here instead of the alias file. The bashrc file loads the alias file into itself. Users typically put their aliases in the alias file, but this is not required.
To create a permanent environment variable, open ~/.bashrc in a preferred text editor. If a user wanted to make a variable that contained the uppercase Greek alphabet, they would set this variable assignment with the other variables being set in this file. To prevent an accidental code mess up in this file, put the new variable at the very end of the file.
After logging out and then back in, the user can now use the variable "UPGREEK" in the terminal. The use for this particular variable is with regex. The user that added this environment variable may not have a keyboard with Greek letters, yet they need to use regex to replace Greek characters with underscores. For instance, the following command will delete uppercase Greek letters:
cat ./some_file | sed -r -e "s|([$UPGREEK].)|_|gI" > ./new_file
Making Aliases and Environment Variables Available to Everyone
There is a bashrc file that is loaded in all user accounts on a Linux system. If a Linux administrator wishes an alias or environment variable to be accessible to everyone, then they can be added to /etc/bash.bashrc. Root privileges are needed to edit the file. It is best to add aliases and variables to the end of the file. This file contains a lot of coding. Adding code to the middle of an important function can ruin the system.
Note: Shell comment marks (#) are valid comment marks in the alias and bashrc files.
Changing the Prompt
In BASH, the command prompt is set up with a variable. The variable is "PS1". If a user types "echo $PS1", then they will see the coding for their prompt. Some Linux systems will show simple syntax while others will display complex coding when the value of this variable is printed. To change the prompt type
"SOMETHING" is the text or code that is desired to be used. Users may use commands to set the variable. For instance, a user could type
PS1="`uname` >> "
The prompt would then be the output of "uname", a command, and two ">" with a space on each side (Linux >> ). To use commands to set the variable, surround the commands with the back-tick (`). This tells BASH that the text in the back-ticks is to be executed.
Users can use special formatting marks to enhance their prompt. For illustration, a user could type
PS1="`uname` \\d >> "
This will make the terminal look like this: "Linux Tue May 28 >> ".
Some special characters (or formatting marks) include the following.
- \a - ASCII bell character
- \d - date format: Wed January 16
- \h - hostname up to the first '.'
- \H - hostname
- \j - number of current jobs run by the shell
- \l - basename of the shell’s terminal device name
- \n - newline
- \r - carriage return
- \s - shell's name
- \t - time in 24-hour HH:MM:SS format
- \T - time in 12-hour HH:MM:SS format
- \@ - time in 12-hour am/pm format
- \A - time in 24-hour HH:MM format
- \u - username
- \v - BASH version
- \V - release of bash, version, and patch level
- \w - current working directory. $HOME is abbreviated with a tilde.
- \W - basename of the current working directory. $HOME is abbreviated with a tilde.
- \! - history number of current command
- \# - command number of current command
- \$ - if the effective UID is 0, then a # is placed here, otherwise a $ is used
- \nnn - character corresponding to the octal number nnn
- \\ - a literal backslash
- \[ - begin sequence of non-printing characters. This can be used to embed a terminal control sequence into the prompt.
- \] - end sequence of non-printing characters
Use a Different Shell
If a user does not like BASH, one of the many shells in Linux, users can change their default shell. To do this, type
chsh -s SHELL, where SHELL is the preferred shell. For illustration, if a user wants to use tcsh instead of BASH, then the user can type
chsh -s tcsh. The command "chsh" means CHange SHell, the "-s" means shell, and tcsh is the shell that the users wishes to use. This will only change the user's shell - no one else's.
These are some of the many ways users can customize BASH and make it easier to use. Other shells can be changed the same or similar ways like csh, ksh, tcsh, zsh, etc. Many shells have their own version of BASH's alias and bashrc files. This article should also help readers understand how to customize and edit other shell environments.