0

I want to run a bash script named hello (just as an example) and want to run it by just typing hello rather than path/hello.sh. The purpose is to run scripts like how we run docker, mysql, git etc commands where you only type the name before the arguments and options rather than typing in the path/executable.extension to run it.

I've followed the top answer to this question: Add a bash script to path

which says (for a script named apt-proxy.sh):

Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.

Add ~/bin to your PATH, typing export PATH=$PATH:~/bin

If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.

Then you can just run apt-proxy with your arguments and it will run anywhere. Note that if you export the PATH variable in a specific window it won't update in other bash instances.

So I made a script named hello (without the .sh) and it just contains:

#!/usr/bin/sh

echo "hello"

it's just in my home/username folder so I added export PATH=$PATH:$HOME/username tho this doesn't make sense as that dir is already in the PATH but I don't know what else to do.

I added it to the ~.profile file as I use ubuntu. Then used source to run/update it.

From the linked post, doing this should supposedly make me able to run hello just like that rather than ./hello, but I can't. I have to type ./hello.

How do I run it as just hello?

braX
  • 11,506
  • 5
  • 20
  • 33
vbnm
  • 45
  • 5
  • 2
    my first guess would be there's some white space in `$PATH:$HOME/username` which is causing your `PATH` to be incomplete; if this is the case ... make sure you wrap the right side of the `=` in double quotes, ie, in your `.profile` make sure you have `PATH="$PATH:$HOME/username"`; if this still doesn't work then please update the question with the complete output from `typeset -p PATH` – markp-fuso Mar 17 '23 at 14:33
  • @markp-fuso Thank you, it works now with the correct syntax. I also though the home folder was added to path by deafult - why I was confused about adding it "again". Anyway, thank you. Btw do you know if the change in PATH is system wide or would I have to change the environment file for that? typeset -p PATH shows: declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin://home/username/username://home/username/username:/home/username:username:/home/username:username" – vbnm Mar 17 '23 at 14:45
  • 1
    if the only change is made in your `.profile` then the change only applies to a session that sources your `.profile` (ie, it's not a system wide change); for system wide changes you'd need to update one or more files in `/etc`, which is going to depend on your shell and/or OS version; your current `PATH` appears to show multilpe attemps to add '`$HOME/username` to `PATH` – markp-fuso Mar 17 '23 at 14:51
  • 1
    `$HOME` typically includes your actual `username` (eg, `echo "$HOME"` ==> `/home/username`) so `$HOME/username` becomes `/home/username/username` ... which would explain why your path may not be working correctly; consider changing your `.profile` to `PATH="$PATH:$HOME"` – markp-fuso Mar 17 '23 at 14:53
  • 1
    'session' => generally speaking ... a login session; this would also get loaded for each terminal/console window you open (ie, each console/window is a new login session); the setting disappears when you logout; the reason you place it in the .`profile` (or `.bashrc`) is so that it's automatically applied each time you login – markp-fuso Mar 17 '23 at 17:36
  • Yes I see now. I deleted the added path in .profile as I would have to reload it in every new terminal window. I added it to .bashrc instead. What's the difference between having added the path in .bashrc compared to if I added it to bash.bashrc in /etc/? The latter would be system wide right? But is the former user specific or what are its limitations? – vbnm Mar 17 '23 at 20:25
  • 1
    most distributions have default configurations whereby `.profile` calls `.bashrc` (or is it vice versa? *shrug*) ... sometimes by the login scripts under `/etc` ... sometimes by the (default) scripts created in a new user's home directory; yes, adding to anythning under `/etc` is going to make it system wide (ie, every account logging into this host will have your home directory in their `PATH`); whether or not this is a 'good' thing depends on your env ... if it's just you on the machine, or you've granted permissions on your home directory to everyone ... this is entirely up to you to decide – markp-fuso Mar 17 '23 at 20:34

1 Answers1

2

I would first try updating your line 1 from:

#!/usr/bin/sh

To reflect the actual path of the bash binary which is:

#!/bin/bash

This will solve your issue if the script is being called properly, but it's interpreter shell is not proper. If the script isn't being called properly, I'd also suggest adding a symbolic link to a directory in your path like so:

ln -sf [/path/to/your/script] $(echo $PATH | awk -F ':' '{print $1}')/[script name you want to call anywhere]