linux shell command line prompt, history, and editing

September 25, 2012

updated 2016-09-20

How to Retrieve and Edit Commands in Linux


All this information assumes the default “bash” shell. If hitting the up arrow does not retrieve the previous command, or the command “history” does not get a list of your previous commands, then you do not have the Bash shell. And, if ctrl-p is the only way to get your previous command, then you are probably in the “sh” shell. look at your passwd file, “less /etc/passwd”, and see if the end of your line entry in the file says “:/bin/sh” or “:/bin/bash”. As root, (su -) edit the file and change sh to bash.

“There are many other shells available. Unless there is a compelling reason not to, it is recommended that people stick to the Bash shell, because this increases the chance that your scripts will be portable between machines, distributions, even operating systems.”
from www.arachnoid.com/linux/shell_programming.html

How to “speed search” for old commands on the command line

You can hit Ctrl-r and type a few letters to see only the command(s) entered that *contain* those letters, the most recent one; hit Ctrl-r again till you get back to the one you want. That is so much faster than hitting the up arrow over and over, going through ALL the commands that you entered. It is also better than typing “history” to see the full list of (hundreds of) commands in the command history buffer.

To see the date and time of all commands when running “history” command

HISTTIMEFORMAT="%y/%m/%d %T "
OR
HISTTIMEFORMAT="%y-%m-%d %T "
put this command in your .bashrc file

To run more than one command

Though it is seldom done, you can execute more than one command at once, actually, in series, on a command line by separating them with a semi-colon “;” More likely, you will want to send output from one to a second command, called a pipe.

A pipeline is a sequence of one or more commands separated by one of the control operators | or |&.
If |& is used, the standard error of command is connected to command2’s standard input through the pipe;
If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates.
A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ⎪⎪, and optionally terminated by “;” or “&” (or a cr)

you can read more about this from the man page, bash “man bash”

How to run commands “in the background”

Type a space and an ampersand “&” after the command and it will run in the background – so that you can be executing other commands while it runs, if you wish.

How to: Change your bash command prompt (PS1)

Note: A script run from the command line is usually run in a non-interactive, non-login shell.
The .bashrc program is called for many non-interactive, batch, command line programs *especially* rsync which I use a lot. and in those situations, several tput errors are generated
(tput: No value for $TERM and no -T specified) . . . if you don’t restrict the instances of it in your .bashrc to interactive sessions, where a prompt is needed.
to test for an interactive session:
# test for a terminal = to test if STDIN is a tty.
if [ -t 0 ];
* OR *
if [[ $- == *i* ]] # check to see if $- contains an i … i=interactive
* OR *
if tty -s;
* OR *
if [ “$PS1” ];
* OR *
[ -z “$PS1” ] && return # implied “if”, not running interactively; return=exit.


if [[ $- == *i* ]]
then
echo interactive
else
echo non-interactive
fi

edit /home/your/.bashrc

PS1 is the prompt variable.

Examples:

PS1="[\A \u@\H \W]\$ "
creates:
[12:15 user@localhost ~]$ ( ~ = home-dir. )
or
[12:15 usre@localhost Desktop]$ ( when in your Desktop folder )
etc.

PS1="[\d \T \u@\H \W]\$ "
creates:
[Tue Sep 20 03:10:55 user@localhost ~]$

PS1="[\D{%F %T} \u@\H \W]\$ "
creates:
[2016-09-20 15:12:42 user@localhost ~]$
note: %F is equivalent to %Y-%m-%d (the ISO 8601 date format)

PS1="[\D{%m-%d} \A \u@\H \W]\$ "
creates:
[09-20 15:20 user@localhost ~]$

PROMPT options

* \d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
* \e : an ASCII escape character (033)
* \h : the hostname up to the first '.'
* \H : the hostname
* \n : newline
* \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 + patch level (e.g., 2.00.0)
* \w : the current working directory, with $HOME abbreviated with a tilde
* \W : the basename of the current working directory, with $HOME abbreviated with a tilde
* \! : the history number of this command
* \# : the command number of this command
* \$ : if the effective UID is 0 (root), 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 characters

http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html


# check to see if $- contains an i ... i=interactive
if [[ $- == *i* ]]
then
BLUE="\[$(tput setaf 4)\]"
RED="\[$(tput setaf 1)\]"
RESET="\[$(tput sgr0)\]"
#
# If id command returns zero, you have root access - you are logged in as root.
if [ $(id -u) -eq 0 ];
then # you are root, set red color in prompt for user@host
PS1="[\t $RED\u@\H $RESET\W]\\$ "
else # normal: same with blue color
PS1="[\t $BLUE\u@\H $RESET\W]\\$ "
fi
fi


the Bash manual says, “For almost every purpose, shell functions are preferred over aliases.”

example:
crude: alias hist=’history 50 | less’ # $1 (any param, $2, $3, etc.) instead of 50, does not work
better: hist() { history $1 | less ; }
functions handle command line “arguments”

 

see a good, one page intro to shell programming

 

 

Leave a Reply

We try to post all comments within 1 business day