How to run a bash file "mytest.sh" on a MAC without using ". /" alias and globally?

Asked

Viewed 54 times

0

I have a file mytest.sh which will be distributed among other users (all with mac). I wanted to know what has to be done so that they can run the script of this file, from anywhere ( globally ) without having to use ./ and sweating only one alias, runtest ?

  • Related: https://answall.com/q/216827/64969

2 answers

3


Has two alternatives.

First: Add the directory that is located your script in the PATH variable to facilitate execution

PATH=$PATH:~/opt/bin:~/dev/bin:/seu/diretorio/

So you’ll just need to type the name of your script from any directory you’re in.

Second: Create an alias, as you yourself suggested.

alias NomeDoAlias='/seu/diretorio/script.sh'

This way you only need to type in the command line the name of your Aliases.

The two commands can be added in the file /etc/profile , because every user who logs in will already load such settings.

  • the first is a good option.

  • 1

    I think that in the first option you should not put the script.sh at the end of the variable $PATH

  • 1

    Really @Jeffersonquesado, thanks for the review. The PATH variable must contain the directory in which the script is located, so all scripts in that directory will be automatically in the PATH. The response has been updated.

  • 1

    @Jjoao was only to give better understanding how directories are added to the PATH variable.

  • @Diego, okay, I see the idea. It’s not relevant but in the Unixeira tradition, /dev/ is usually for Developers and there is usually no bin there, and the /opt/... are not usually in the user areas.

1

I see two orthogonal questions here:

  • for easy use of a script, recommend installing the script in a PATH folder (example sudo cp script /usr/local/bin for general use; or cp script ~/bin for personal use)

  • to avoid the ./..., we can merge the current folder to the path

export PATH=$PATH:.

(eventually attach this line to the file ~/.bashrc to be activated each time we enter)

The second option is great for personal machine but should be avoided if we work on a hostile machine (example if we are administrator system with users who like to try to trap commands...)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.