Bash

From SHellium Wiki
(Redirected from MAN bash)
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

To run a command in the background:

 command &

To run multiple commands on one line, use a separator:

 command1 ; command2 - will run command1, and when command1 is finished it will run command2
 command1 & command2 - will run command1 in the background and run command2 (while command1 is still running)

To run a command (command2) only if another command (command1) succeeds:

 command1 && command2

To search command history, use Ctrl+r
To use the output from one command as the input for another, use a pipe:

 ls | grep myfile

For more information, including shell programming (loops, conditionals, variables, etc):

 man bash

Editing the prompt

With Bash, you are able to edit your command prompt. You can show the time, current working directory, hostname, username, and/or dress it up with eyecandy.

First off, find the .bashrc file in your home directory. Open it up with vim or whatever editor you prefer. Somewhere in there, there should be a "PS1="codehere"". This is what makes up the bash prompt. Most likely, you'll see something along the lines of this:

 PS1="\\u@\h \\W]\\$"

This outputs to something like this:

What most users have set as their default prompt


However, if I were to set my PS1 to something like "PS1="command prompt $ "" then the prompt would suddenly look like this:

a modified prompt

NOTE: The command prompt ends wherever it is that you end the quote mark.

Here is a list of some variables to put into your prompt.

\a : An ASCII bell character (07)
\d : the date in "Weekday Month Date" format (e.g., "Mon June 30")
\D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; and empty format results in a locale-specific time representation. The braces are required.
\e : an ASCII escape character (033)
\h : the hostname up the the first "."
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the new basename of the shell's terminal device name
\n : new line (for example, if I wanted to have my hostname in the top line, and my working directory in a second line, I would do "\H \\n \w"
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final slash)
\t : the current time in 24=hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour AM/PM format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version, and patch level (e.g., 2.00.0
\w : the current working directory, with $HOME abbreviated as a tilde
\W : the basename of the current working directory, with $HOME abbreviated as a tilde
\! : the history number of this command
\# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\\ : a backslash
\[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] : end a sequence of non-printing numbers


For example, here's my PS1:

 PS1="┌─[\A][\u@\h:\w]\n└─> "

Which turns into this:

my configured bash prompt

Now, by default, the color of the terminal draws directly from the xdefaults file. If you don't have access to the xdefaults file, there is still a way to make your terminal the color that you want.

Assuming your default prompt is something like PS1="\u@\h \W]\$", to make it green, make it PS1="\[\e[1;32m\]\u@\h \W\$\[\e[0m\]". Here is the result:

a green prompt

Notice the [\e[0m\] at the end. This signifies that you want to stop using that color. By doing this, you can make any part of your prompt any color you want.

Here's a list of the colors and their codes.

 txtblk= '\e[0;30m' # Black - Regular
 txtred= '\e[0;31m' # Red
 txtgrn= '\e[0;32m' # Green
 txtylw= '\e[0;33m' # Yellow
 txtblu= '\e[0;34m' # Blue
 txtpur= '\e[0;35m' # Purple
 txtcyn= '\e[0;36m' # Cyan
 txtwht= '\e[0;37m' # White
 bldblk= '\e[1;30m' # Black - Bold
 bldred= '\e[1;31m' # Red
 bldgrn= '\e[1;32m' # Green
 bldylw= '\e[1;33m' # Yellow
 bldblu= '\e[1;34m' # Blue
 bldpur= '\e[1;35m' # Purple
 bldcyn= '\e[1;36m' # Cyan
 bldwht= '\e[1;37m' # White
 unkblk= '\e[4;30m' # Black - Underline
 undred= '\e[4;31m' # Red
 undgrn= '\e[4;32m' # Green
 undylw= '\e[4;33m' # Yellow
 undblu= '\e[4;34m' # Blue
 undpur= '\e[4;35m' # Purple
 undcyn= '\e[4;36m' # Cyan
 undwht= '\e[4;37m' # White
 bakblk= '\e[40m'   # Black - Background
 bakred= '\e[41m'   # Red
 badgrn= '\e[42m'   # Green
 bakylw= '\e[43m'   # Yellow
 bakblu= '\e[44m'   # Blue
 bakpur= '\e[45m'   # Purple
 bakcyn= '\e[46m'   # Cyan
 bakwht= '\e[47m'   # White
 txtrst= '\e[0m'    # Text Reset

NOTE: You will need to have a color-enabled terminal for colors to work.

Now you know all you need to make an awesome bash prompt!

If you're having trouble coming up with your own prompt, or just want to see what others have come up with, there is an awesome thread over at the Arch Forums made for that exact reason.

Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox