Is there a way to add an alias from the command line without directly editing the bash_aliases file? To clarify, how can I make a bash script do it without having to enter something along the lines of nano bash_aliases to add them?


Assuming you are using bash_aliases (it is not necessary, you can also have aliases defined in .bashrc among other places), you can simply add a line to the file:

printf "alias foo='bar'" >> ~/.bash_aliases

Alternatively, if you only want this alias for the current session, use the alias command directly:

alias foo='bar'


Bash does not allow aliases to be expanded (to work) in scripts by default, you will need to activate the expand_aliases option:

#!/usr/bin/env bash

alias foo='echo "It works!"'
echo "  Alias defined, attempting to use without expand_aliases"

shopt -s expand_aliases
echo "  Attempting to use with expand_aliases"

If I run the script above, the alias foo will only work after I have activated the expand_aliases option:

$ a.sh
  Alias defined, attempting to use without expand_aliases
/home/terdon/scripts/a.sh: line 5: foo: command not found
  Attempting to use with expand_aliases
It works!
  • so if I run shopt -s expand_aliases, is it permanent? As long as I don't totally reinstall the OS, it'll stay? I ask because I'm on a Raspberry Pi; some things reset when I reboot. – evamvid Feb 1 '14 at 18:39
  • Also, to confirm, if I just use the alias command, it will also reset once I reboot? – evamvid Feb 1 '14 at 18:40
  • @evamvid no, it will only work for the particular session. What are you trying to do? Is this something you want to do once in a script? Everytime you boot? Do you want the alias to be available to the script? In the normal session? Please explain what your end objective is. – terdon Feb 1 '14 at 18:40
  • I'm trying to set up a node.js blogging platform called Ghost on my Pi. It has a pretty long, fiddly command to start up in production mode. I wanted to make an alias so that I could just put in startghost for it to start. – evamvid Feb 1 '14 at 18:46
  • 1
    @evamvid I don't understand. Just manually edit bash_aliases once, add the relevant alias and you're done, it will be available for ever. You don't need to do this on each boot. What would the script be for? – terdon Feb 1 '14 at 18:51

Just execute the alias as you would in bash_alias.

alias lt='ls -altrch'

Check by using type command.

type alias
lt is aliased to 'ls -altrch'
  • Whoa! You can do that? =) – evamvid Feb 1 '14 at 18:32
  • 1
    Note that this will not work in a script unless you activate expand_aliases, see my answer. – terdon Feb 1 '14 at 18:34

You can also use HEREDOCS to add multiple lines as well:

cat <<EOF
alias ali1='cmds1...'
alias ali2='cmds2...'
) >> ~/bash_aliases

The above can be copy pasted into a shell.


$ more ~/bash_aliases 
alias ali1='cmds1...'
alias ali2='cmds2...'

Alternatively, you could just make a new script to do the startup. Create a file, /usr/local/bin/start_node_blog with the following contents:



Then do

chmod +x /usr/local/bin/start_node_blog

and you can run it after a reboot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.