[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
howtos:cli_manual:shells [2012/10/03 20:57 (UTC)] – [Configuring a shell prompt - PS1] sycamorexhowtos:cli_manual:shells [2012/10/28 21:07 (UTC)] – [~/.bashrc] sycamorex
Line 119: Line 119:
     . ~/.bashrc     . ~/.bashrc
 fi</file> fi</file>
 +
 +If you are going to do any customisations of root's environment (eg. configuring root's shell prompt discussed below), you need to do the above for root user as well.
  
 2. Place all your customisations in ''~/.bashrc''. 2. Place all your customisations in ''~/.bashrc''.
Line 172: Line 174:
 export MY_EXPORTED_VAR="More text"</code> export MY_EXPORTED_VAR="More text"</code>
  
-By exporting a variable you make it accessible to any sub-shells created in the current shell. In other words, if you want a script to be aware of a variable, you need to export it.+By exporting a variable you make it accessible to any sub-shells created in the current shell. In other words, if you want a script to be aware of a variable, you need to export it. If you want to see all the exported variables in the current shell, type: 
 +<code>export -p</code>
  
  
Line 221: Line 224:
 /home/user/data/projects/slackbuilds/yajl</code> /home/user/data/projects/slackbuilds/yajl</code>
  
-====Configuring a shell prompt ====+==== Set the Default Editor ==== 
 + 
 +For historical reasons, there still exist 2 separate environment variables (''$VISUAL'' and ''$EDITOR'') responsible for specifying the default command line editor. You can use any of them or better still, assign the same value to both of them. Bash chooses the default editor by checking the ''$VISUAL'' and then ''$EDITOR''. If neither of them is set, it opens ''Emacs''. Add the following to ''~/.bashrc'' (Obviously replace ''vim'' with an editor of choice): 
 + 
 +<code>export VISUAL=vim 
 +export EDITOR=vim</code> 
 + 
 +====Configuring a Shell Prompt ==== 
 When you first open a terminal window in Slackware, you are likely to see the following shell prompt: When you first open a terminal window in Slackware, you are likely to see the following shell prompt:
  
Line 291: Line 302:
 The above shell prompt will also display the number of files and directories in the current working directory. It is important, however, to remember that the command prompt is there to help you. For that reason, one needs to keep the balance between keeping the prompt informative and practical at the same time. A very descriptive, but long, command prompt may easily hinder your productiveness.  The above shell prompt will also display the number of files and directories in the current working directory. It is important, however, to remember that the command prompt is there to help you. For that reason, one needs to keep the balance between keeping the prompt informative and practical at the same time. A very descriptive, but long, command prompt may easily hinder your productiveness. 
  
-For more fancy and colourful prompts, see [[howtos:cli_manual:fancy_prompt|this]].+===Prompt in Colours=== 
 + 
 +One way to display the shell prompt in colour is to use the ''tput'' command which checks the terminfo database and generates relevant codes for a terminal. It has a number of capabilities including: 
 + 
 +  * ''setaf'' - set ANSI foreground 
 +  * ''setab'' - set ANSI background  
 + 
 +Its syntax is as follows: 
 + 
 +^Command^Description| 
 +|tput setaf colour_code| Set a foreground colour |  
 +|tput setab colour_code| Set a background colour | 
 +|tput sgr0             | Switch off any colours| 
 + 
 + 
 +===Colour Codes=== 
 + 
 +The values of colour codes are as follows: 
 + 
 +^Colour^Colour Code| 
 +|black|0| 
 +|red|1| 
 +|green|2| 
 +|yellow|3| 
 +|blue|4| 
 +|magenta|5| 
 +|cyan|6| 
 +|white|7| 
 + 
 +===Configure the Prompt in Colour=== 
 + 
 +Let us define some colour variables in ''~/.bashrc'': 
 + 
 +<code> 
 + 
 +# Defining foreground variables 
 +P_BLACK="\[$(tput setaf 0)\]" 
 +P_RED="\[$(tput setaf 1)\]" 
 +P_GREEN="\[$(tput setaf 2)\]" 
 +P_YELLOW="\[$(tput setaf 3)\]" 
 +P_BLUE="\[$(tput setaf 4)\]" 
 +P_MAGENTA="\[$(tput setaf 5)\]" 
 +P_CYAN="\[$(tput setaf 6)\]" 
 +P_WHITE="\[$(tput setaf 7)\]" 
 +P_RESET="\[$(tput sgr0)\]"</code> 
 + 
 +<note>Please note that we wrap ''\[...\]'' around the output of ''tput'' to indicate that it contains non-printable characters. This helps avoid any problems with calculating line wraps.</note> 
 + 
 +Having defined the colour variables, we can go on to configure the prompt: 
 + 
 +<code>PS1="$P_BLUE\u@\h: \w \\$ $P_RESET"</code> 
 + 
 +This will produce a blue prompt. Please note that we need to turn off any colours at the end to prevent the commands we type from being blue as well.  
 + 
 +The next prompt gives each section of the prompt a different colour: 
 + 
 +<code>PS1="$P_GREEN\u$P_RED@$P_YELLOW\h:$P_CYAN\w$P_BLUE\\$ $P_RESET"</code> 
 + 
 +{{howtos:cli_manual:colour_prompt.png?nolink}} 
 + 
 +Bear in mind that your output might look differently depending on a terminal and its colour settings in eg. ''~/.Xdefaults''
 + 
 +===Root Prompt=== 
 + 
 +It is useful to give the ''root'' user a different prompt (the ''#'' character will be red) by adding the following (+ colour definitions) to the root's ''.bashrc'': 
 + 
 +<code>PS1="$P_GREEN\u$P_RED@$P_YELLOW\h:$P_CYAN\w$P_RED\\$ $P_RESET"</code> 
 + 
 +Do not forget to source root's ''.bashrc'' in its ''.bash_profile'' to make sure it works for both non-login (''su'') and login (''su -'') shells. 
 + 
 +===Prompt Background Colour=== 
 + 
 +If you'd like to set the prompt's background colour, you can also add the ''setab'' capability. Here's an example (without the use of variables): 
 + 
 +<code>PS1="\[$(tput setab 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"</code>
  
  
Line 299: Line 384:
 <code>export PS1="\u@\h \! \w\$ "</code> <code>export PS1="\u@\h \! \w\$ "</code>
  
-=====Building aliases=====+=====Building Aliases=====
  
 +
 +Aliases are shortcuts or abbreviated commands used in a shell in order to avoid typing long commands. Aliases are usually created to modify existing commands by adding some flags or to join a few commands in order to create new custom commands.
 +
 +An example of an alias would be:
 +
 +<code>alias ll='ls -l'</code>
 +
 +Now typing ''ll'' calls the ''ls'' command with the ''-l'' (or ''long listing format'') flag.
 +
 +Another example would be creating a shortcut to query installed packages on a Slackware system:
 +
 +<code>alias qp='ls /var/log/packages | grep'</code>
 +
 +<code>user@darkstar:~$ qp emacs
 +emacs-24.2-x86_64-1</code>
 +
 +The general syntax of an alias is:
 +
 +<code>alias name='full command'</code>
 +
 +To create a temporary alias for the current session, just define it directly on a command line. It will not be remembered when you start another session. To store an alias permanently to be accessible by the shell in the future, place it in ''~/.bashrc''.
 +
 +If you need to (temporarily) switch off an alias you can use the ''unalias'' built-in:
 +
 +<code>user@darkstar:~$ qp emacs
 +emacs-24.2-x86_64-1
 +user@darkstar:~$ unalias qp
 +user@darkstar:~$ qp
 +bash: qp: command not found</code>
 +
 +If you run a (long) command on a regular basis, it might be convenient to create an alias for it.
 +
 +Have a look at some more examples of aliases below. This may help you create your own aliases.
 +
 +<note important>Please remember that creating aliases that replace system commands may introduce security risks so always consider creating a new command instead.</note>
 +
 +<code>alias e='exit'
 +alias ll
 +alias lla='ls -al | less'
 +alias mkdir='mkdir -pv' 
 +alias rm='rm -i' 
 +alias 1.='cd .. ; pwd'
 +alias 2.='cd ../.. ; pwd'
 +alias 3.='cd ../../.. ; pwd'
 +alias 4.='cd ../../../.. ; pwd'
 +alias h='history'
 +alias eq='alsamixer -D equal'
 +
 +alias emc='emacs -nw'
 +alias org='emacs -nw /home/user/data/todo.org'
 +alias ci3='vim /home/user/.i3/config'
 +
 +alias psp='ps aux | grep'
 +alias wic='nmap -sP 192.168.1.0/24'
 +
 +alias dl='cd /home/user/data/downloads/ ; ls'
 +alias mftp='lftp sftp://user@ftp.server.com'
 +alias mylaptop='ssh user@ip_address'
 +alias myserver='ssh user@ip_address'
 +
 +alias t='task'
 +alias r2j='mkdir jpg; ufraw-batch --out-type=jpeg --out-path=./jpg ./*.NEF'
 +</code>
 +
 +=== Aliases and Security ===
 +Sometimes aliases may pose a security risk in a sense that they can spoof other commands (eg. on compromised systems). Consider the following:
 +
 +<code>alias ls='some_nasty_command'</code>
 +
 +To view currently set aliases, just type ''alias'' at the command prompt. To clear all the aliases, type:
 +
 +<code>\unalias -a</code>
 +
 +Escaping the command with \ prevents any alias expansion in case someone had tried to spoof the ''unalias'' command. See the following:
 +
 +<code>user@darkstar:~$ pwd
 +/home/user
 +user@darkstar:~$ alias pwd='echo 666'
 +user@darkstar:~$ pwd
 +666
 +user@darkstar:~$ \pwd
 +/home/user</code>
 =====Creating functions===== =====Creating functions=====
 +
 +FIXME
 +=====~/.bashrc=====
 +
 +At the moment our ''~/.bashrc'' looks as follows. At the bottom you'll see some additional settings that have not been discussed. The comments above them should clarify their meaning.
 +
 +<code>
 +# load the system-wide environment
 +source /etc/profile
 +
 +# Add a directory with your scripts to the path.
 +PATH=$PATH:~/bin
 +
 +# Configure the CDPATH variable to include a frequently visited directories
 +CDPATH=$CDPATH:~/data/projects/slackbuilds/
 +
 +# Set the default editor
 +export VISUAL=vim
 +export EDITOR=vim
 +
 +# Defining foreground variables for the prompt
 +P_BLACK="\[$(tput setaf 0)\]"
 +P_RED="\[$(tput setaf 1)\]"
 +P_GREEN="\[$(tput setaf 2)\]"
 +P_YELLOW="\[$(tput setaf 3)\]"
 +P_BLUE="\[$(tput setaf 4)\]"
 +P_MAGENTA="\[$(tput setaf 5)\]"
 +P_CYAN="\[$(tput setaf 6)\]"
 +P_WHITE="\[$(tput setaf 7)\]"
 +P_RESET="\[$(tput sgr0)\]"
 +
 +# Setting a fancy prompt for the current user
 +export PS1="$P_GREEN\u$P_RED@$P_YELLOW\h:$P_CYAN\w$P_BLUE\\$ $P_RESET"
 +
 +# Setting aliases
 +alias e='exit'
 +alias ll='ls -l'
 +alias lla='ls -al | less'
 +alias mkdir='mkdir -pv' 
 +alias rm='rm -i' 
 +alias 1.='cd .. ; pwd'
 +alias 2.='cd ../.. ; pwd'
 +alias 3.='cd ../../.. ; pwd'
 +alias 4.='cd ../../../.. ; pwd'
 +alias h='history'
 +alias eq='alsamixer -D equal'
 +
 +alias emc='emacs -nw'
 +alias org='emacs -nw /home/user/data/todo.org'
 +alias ci3='vim /home/user/.i3/config'
 +
 +alias psp='ps aux | grep'
 +alias wic='nmap -sP 192.168.1.0/24'
 +
 +alias dl='cd /home/user/data/downloads/ ; ls'
 +alias mftp='lftp sftp://user@ftp.server.com'
 +alias mylaptop='ssh user@ip_address'
 +alias myserver='ssh user@ip_address'
 +
 +alias t='task'
 +alias r2j='mkdir jpg; ufraw-batch --out-type=jpeg --out-path=./jpg ./*.NEF'
 +
 +# Additional settings:
 +
 +# Specify an NNTP Server 
 +export NNTPSERVER='aioe.org'
 +
 +
 +CHOST="x86_64"
 +export CFLAGS="-march=native -O2 -pipe -fPIC"
 +export CXXFLAGS="${CFLAGS}"
 +export PATH=$PATH:~/bin
 +export MAKEFLAGS="-j4"
 +export QUEUEDIR=/home/xtd8865/data/queues
 +
 =====Other configuration files===== =====Other configuration files=====
  
 howtos:cli_manual:shells ()