0

Issue

I want to run a bash file more easily, I've seen some applications where you only need to type word to execute the script.

Instead of typing ~/folder/file.sh in the terminal, I only have to type a_word to run the file.

Is this possible with bash?

And also, this is on RPiOS's terminal, not sure if it differs.

2
  • use an alias. googe bash alias Commented Aug 27, 2021 at 17:28
  • @RedCricket, ...that's awful advice. See wooledge.org/~greybot/meta/alias -- notice how for years the #bash IRC channel gave as advice when anyone asked about aliases "use a function instead". Commented Aug 27, 2021 at 17:47

2 Answers 2

1

Save your file to a location named in PATH. /usr/local/bin/a_word (no .sh) is a great example of such a location. Make sure it has executable permissions and starts with a shebang (like #!/usr/bin/env bash).

When you want to install something just for your own account, it's common practice to create a ~/bin directory and add it to your PATH (as by adding something like PATH=$PATH:$HOME/bin in ~/.bash_profile).

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

Comments

0

You have to define a so called alias.

Edit the file $HOME/.bashrc and add alias a_word = '$HOME/folder/file.sh', then logout and logon again.

3 Comments

thanks so much! I will make this the answer when it lets me.
Aliases are horrible and should never be used by anyone. They don't work in lots of contexts -- you can't use find -exec some-alias or run :! some-alias in vim; and they're turned off entirely in scripts.
When one doesn't want to use a script, it's still better to use a function than an alias. a_word() { "$HOME/folder/file.sh" "$@"; } -- functions can be exported to the environment, they work in scripts, they allow branching logic and let arguments be evaluated in non-final positions. The only time an alias is better than a function -- and it's a very rare case -- is if you want to expand to something that uses shell syntax in a way that's illegal without extra content the user is expected to type after the alias itself.