2

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?

4 Answers 4

5

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'

Important

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"
foo

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

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!
7
  • 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, 2014 at 18:39
  • Also, to confirm, if I just use the alias command, it will also reset once I reboot?
    – evamvid
    Feb 1, 2014 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, 2014 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, 2014 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, 2014 at 18:51
1

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'
2
  • Whoa! You can do that? =)
    – evamvid
    Feb 1, 2014 at 18:32
  • 1
    Note that this will not work in a script unless you activate expand_aliases, see my answer.
    – terdon
    Feb 1, 2014 at 18:34
1

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

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

The above can be copy pasted into a shell.

Results

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

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:

#!/bin/bash

that_long_fiddly_command_you_mentioned

Then do

chmod +x /usr/local/bin/start_node_blog

and you can run it after a reboot.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .