[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
howtos:cli_manual:shells [2012/10/06 21:50 (UTC)] – [Configuring a shell prompt] sycamorexhowtos:cli_manual:shells [2012/10/28 21:39 (UTC)] (current) – [~/.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 228: Line 231:
 export EDITOR=vim</code> export EDITOR=vim</code>
  
-====Configuring a shell prompt ====+====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 367: Line 370:
 <code>PS1="$P_GREEN\u$P_RED@$P_YELLOW\h:$P_CYAN\w$P_RED\\$ $P_RESET"</code> <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 ''su'' and ''su -''.+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=== ===Prompt Background Colour===
Line 381: 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'
 +
 +# To take advantage of multicore CPUs you can use the MAKEFLAGS variable. 
 +# For example the equivalent of "make -j2" would be:
 +# export MAKEFLAGS="-j2"
 +# Uncomment the above line to use it.
 +
 +</code>
 +
 =====Other configuration files===== =====Other configuration files=====
  
 howtos:cli_manual:shells ()