Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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!
share|improve this answer
    
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 at 18:39
    
Also, to confirm, if I just use the alias command, it will also reset once I reboot? –  evamvid Feb 1 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 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 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 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'
share|improve this answer
    
Whoa! You can do that? =) –  evamvid Feb 1 at 18:32
    
Note that this will not work in a script unless you activate expand_aliases, see my answer. –  terdon Feb 1 at 18:34

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...'
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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