1

I have a bash script that I would like to run globally as a command. For that I moved the script to /usr/local/bin/somefile.sh.

Now I still have to call a command called somefile.sh. I would like to have an alias for this command (for example I would just like to call the script with the command sf).

How do I do that?

3
  • 1
    Easiest solution, rename it to sf. Commented Feb 25, 2019 at 13:04
  • I already used the aliases. Would it still execute, if it is no longer an .sh file? If it has not file ending? Commented Feb 25, 2019 at 15:43
  • Yes. In fact, it's generally recommended not to give scripts a .sh extension. Commented Feb 25, 2019 at 16:58

4 Answers 4

2

And to complement answer from @Kent :

alias sf='/usr/local/bin/somefile.sh' using alias

ln -s somefile.sh /usr/local/bin/sf using soft link

mv /usr/local/bin/somefile.sh /usr/local/bin/sf using renaming

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to make the definition of the alias permanent. I don`t want to retype it after every restart of my computer.
2

There are different ways:

  • as you mentioned, use alias, man alias for details
  • create a soft link named sf to your real script
  • rename your script

2 Comments

Is there a way to make the definition of the alias permanent. I don`t want to retype it after every restart of my computer.
@User12547645 add the alias to .bashrc file.
2

Open your .bashrc file located in ~/.bashrc. .bashrc file is read whenever you login to the system. Add the below line to the end of the file.

alias sf='/usr/local/bin/somefile.sh'

then re login or run source .bashrc

Comments

1

If you're on a GNU system, you could use the "alternatives" system:

dir=/usr/local/bin
sudo update-alternatives --install $dir/sf somefile $dir/somefile.sh 10

This creates 2 symbolic links:

/usr/local/bin/sf -> /etc/alternatives/somefile
/etc/alternatives/somefile -> /usr/local/bin/somefile.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.