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.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74

2 Answers2

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).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
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.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • thanks so much! I will make this the answer when it lets me. –  Aug 27 '21 at 17:30
  • 1
    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. – Charles Duffy Aug 27 '21 at 17:42
  • 1
    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. – Charles Duffy Aug 27 '21 at 17:45