1

I just wants to modify a bash shell command like exit so when i write exit in the terminal it clears the screen, and echo some text, wait 2 seconds, then execute the exit function.
So is there any way to modify a shell command on Ubuntu, if there is away how ?

Black Block
  • 4,991

3 Answers3

4

You can use a shell alias

alias exit='clear; sleep 2; exit'

To make it permanent, add the alias to the bottom of the ~/.bashrc file. see this thread for help

steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • isn't this going to override the original exit command, i mean that making exit='sleep 2; exit' the second exit is not going to be known for the bash and the terminal will not do the exit function.(i ask before do it and then find out that it is not going to work) – Black Block Apr 01 '15 at 23:44
  • 3
    @Naruto no that shouldn't happen: in fact, you probably already have several such aliases already defined by your default ~/.bashrc file, such as alias ls='ls --color=auto' and alias grep='grep --color=auto' (type alias without any arguments to see a list of the current ones). – steeldriver Apr 01 '15 at 23:54
  • @Naruto - the problem here is that exit is a bash builtin - for a normal command where you wanted it to do something similar you probably could enter the command's full path (e.g. alias firefox='clear; sleep 2; /usr/bin/firefox'). The above may work anyway :). An alternative to exit that may work would be something along the lines of kill -SIGINT $$, kill -SIGKILL $$ to kill the process's PID (SIGKILL works but does not exit cleanly, other stuff seems to get ignored by the terminal - stuff on signals here: http://en.wikipedia.org/wiki/Unix_signal) – Wilf Apr 01 '15 at 23:57
  • can i revert this command to get the original exit command back, also can i make another command the same way you answer here like (alias exit2='clear; sleep 2; exit') or how to do it ? – Black Block Apr 02 '15 at 00:02
  • You can reverse it in the current shell using the unalias command e.g. unalias exit. You can execute the original command without unaliasing by preceding the command with a backslash, i.e. \exit. – steeldriver Apr 02 '15 at 00:21
1

I have found a workaround instead of editing the exit bash command,
trap 'clear; ~/ascii3.sh; spd-say "Exit"; sleep 2' EXIT by using a trap to the exit in the terminal and i put it in the end of the .pashrc file and it works.
And the ascii3.sh:

echo -e "\033[01;31m"
echo " _  _   __   _  _  ____     __     __ _  __  ___  ____    ____   __   _  _  _   "
echo "/ )( \ / _\ / )( \(  __)   / _\   (  ( \(  )/ __)(  __)  (    \ / _\ ( \/ )/ \  "
echo ") __ (/    \\\ \/ / ) _)   /    \  /    / )(( (__  ) _)    ) D (/    \ )  / \_/  "
echo "\_)(_/\_/\_/ \__/ (____)  \_/\_/  \_)__)(__)\___)(____)  (____/\_/\_/(__/  (_)  "

I think the question i asked was a very bad one as it didn't reflect my thoughts but i won't change it nor the answer i just put this answer here for anyone who want it.

Black Block
  • 4,991
0

You cannot easily modify a command, but you can replace a command.

# You only need this one time:
mkdir --mode=755 $HOME/bin  

# You need this command once per login (or in $HOME/.bashrc)
PATH="$HOME/bin:$PATH"  

Then any executable file/script in $HOME/bin will override any command with the same name.

When you type a command, the shell looks for an executable file by that name in each of the directories in $PATH.

Unfortunately, your example exit is a "Shell builtin" (see man bash) and is not sought along $PATH, rather it is handled by the shell internally. To override exit you will have to define a shell function or alias(see man bash) in your $HOME/.bashrc

waltinator
  • 36,399