How to make an alias and work in sh for all users (not in bash)?

07
2014-07
  • Thanasis Petsas

    My question maybe is silly, but how can I make an alias so as to work in sh for all users?

    I know that I can put an alias, let say this one:

    alias ls='ls -l'
    

    in /etc/bash.bashrc so as to work in bash but what is the equivalent for the sh?

    I tried to put is in /etc/profile but doesn't work.

  • Answers
  • Sepahrad Salour

    After put your alias in /etc/profile exec follow command:

    source /etc/profile

    Try this solution and feedback here.


  • Related Question

    osx - Why does an alias work in the Terminal, but not when called from a script?
  • Michael Prescott

    I've added the following to my ~/.bash_profile

    # opens "flashlog.txt" in Console
    alias trace='open -a /Applications/Utilities/Console.app/ ~/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt'
    
    # clears "flashlog.txt"
    alias cleartrace='cat /dev/null > ~/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt'
    

    So, in Terminal I can enter the command "trace" and view the flashlog.txt in the Console. I can also enter the command "cleartrace" and the flashlog.txt is cleared. These work great.

    However, if I create a new bash script with the following I get an error "cleartrace: command not found":

    #!/bin/bash
    cleartrace
    cp -v -f ActivityLauncher.swf ../launchers/addu02l05_launcher_1.swf
    open "/Applications/Adobe Flash CS4/Players/Flash Player.app" ./test.swf 
    

    Why does an alias work in the Terminal, but not when called from a script? (How do I fix it?)


  • Related Answers
  • KeithB

    This is because bash only reads ~/.bash_profile for interactive shells. Move your alias definitions to ~/.bashrc and it should work. Take a look at the INVOCATION section of the bash manpage for more details on how this all works.

  • Dennis Williamson

    The Bash info file says:

    For almost every purpose, shell functions are preferred over aliases.

    Functions can be exported, for one thing.