25

I'm used to putting common scripts in /usr/local/bin so that I can execute them from anywhere with the terminal.

For example, I make a shell script named 1, make it executable with chmod +x 1 and put it in /usr/local/bin, and inside the script I type #!/bin/sh on the first line, and then my commands. From there on, it's very conveniently usable and quick to execute by typing

1Enter

on the terminal, from inside any folder.

My problem is that I'm currently working on a computer where can't do sudo and I can't expect to get it either, so I can't place my script in /usr/local/bin.

What are my options? Is there another path with the same "run from anywhere" capability, which I can access without sudo, or another way to achieve something equivalent?

The accepted answer to this post says

For user-scoped scripts, use bin/ in your home directory.

Which I tried, but there is no bin folder in my home directory, and when I created one, I still could not run the script from anywhere else.

I'm running on Ubuntu 12.04 LTS.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 2
    Don't use 1 or something like that as script name. Try to calculate something in your shell: e.g. expr 1 + 1 (and if not you are doing it, some script will do it at some point for sure) – frlan Jul 01 '15 at 08:27
  • 1
    @frlan: How would expr 1 + 1 be affected by the presence of a script named 1? – psmears Jul 01 '15 at 20:39
  • True... not affecting expr, nor bash arithmetic $(( 1 + 1 )). – Campa Jul 08 '15 at 09:10

4 Answers4

23

What are my options? Is there another path with the same "run from anywhere" capability, which I can access without sudo, or another way to achieve something equivalent?

How to do it?

Create some dir in your home to hold your scripts normally named as bin as convention.

mkdir ~/bin

Now move your scripts to bin

mv somescript ~/bin

Now how to make it tun from everywhere?!

You have to add the bin to the PATH

open your .bashrc

gedit .bashrc

and add this line:

export PATH=$PATH:/home/username/bin

Don't forget to replace username with your User Name

Save and exit, then source the bashrc

source .bashrc

and now you are fine, you can run your script as you used to do! but you have to notice this is related to your user only.

Note: It's better to rename your scripts other than 1 ,2 since you may face some issues with that names


UPDATE:

You can do same just create the bin dir in your home then source ~/.profile instead of ~/.bashrc. Since adding the ~/bin to your PATH is already listed in .profile

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
Maythux
  • 84,289
  • I use export PATH=$PATH:$HOME/bin so I can use the same bashrc for other accounts with different user names. – Anthony Geoghegan Jul 01 '15 at 08:23
  • 1
    Just to mention ~/bin is in $PATH by default on Ubuntu. Just run source ~/.profile or log out/in after you created the directory. – Jacob Vlijm Jul 01 '15 at 08:26
  • @JacobVlijm In which version?!!! I use 12.04, 14.04, 15.04 and none of them has ~/bin in PATH – Maythux Jul 01 '15 at 08:29
  • In all of them, I always use it, in many of my (accepted) answers as well, never got any comment on that, works her at home on five systems as well, but, as said, after running source ~/.profile . See (e.g.) here: http://askubuntu.com/a/247422/72216 – Jacob Vlijm Jul 01 '15 at 08:33
  • @JacobVlijm some illusion here, I have 5 physical machines and more than10 VM and all of them has no ~/bin in PATH, I'm just making checks now and as I said. http://askubuntu.com/questions/402353/how-to-add-home-username-bin-to-path , http://askubuntu.com/questions/9848/what-are-path-and-bin-how-can-i-have-personal-scripts – Maythux Jul 01 '15 at 08:38
  • I think you are the only one : ), I am pretty sure. Did you try creating it, add an executable, log out/in and run it? – Jacob Vlijm Jul 01 '15 at 08:40
  • Nope just run echo $PATH – Maythux Jul 01 '15 at 08:41
  • I think what you say is related to .profile and you should source .profile not .bashrc, since in .profile you can find same as done above in .bashrc, so either do it manually as above or just create bin dir in home and source .profile and not .bashrc – Maythux Jul 01 '15 at 08:47
  • @Maythux Ubuntu's default ~/.profile adds ~/bin to the $PATH if it exists. Since this is in ~/.profile, you need to start a login shell or login graphically for it to take effect. – terdon Jul 01 '15 at 08:50
  • @terdon yes I know that, I was in argue with @jacob since he said it's already by default if just source bashrc, which is the not the case since .profile add this not .bashrc. I already stated that, refer to the update and past comments – Maythux Jul 01 '15 at 08:53
  • 1
    He said source ~/.profile, he never said source ~/.bashrc. :) Also, as a general rule, environmental variables should go in .profile and not .bashrc since they only need to be set up once. (none of this is to say that your answer is not good, mind you, and +1 from me) – terdon Jul 01 '15 at 08:58
  • Note that some distributions automatically add ~/bin to the PATH if it exists at login without you having to do anything else. Ubuntu does, for instance. Might be worth adding to your answer. – Pepijn Schmitz Jul 01 '15 at 15:56
4

In addition to https://askubuntu.com/a/643030/218015 you might can also define an alias inside your .bashrc for small, often used tasks. E.g.

alias ll='ls -l'
alias ls='ls --color=auto'

will create you a "command" ll, which is doing ls -l and ls will be coloured after defining the alias. https://wiki.ubuntuusers.de/alias is having some more examples and a howto for setting it up.

frlan
  • 1,030
  • 5
  • 17
3

Try ~/.local/bin/.

(I acknowledge this answer isn't adding anything new but there's a short answer to this question that shouldn't require reading everything else.)

0

There really should be a per-user bin because it would be really helpful in multi user environments, where people are denied root privledges in order to make everyone more safe. Use the following in a bash script to automatically create it.

if [ ! -d "$HOME/.local/bin" ] ; then
  mkdir -p "$HOME/.local/bin"
  printf '\nexport "PATH=$PATH:'"$HOME"/.local/bin'"' >> "$HOME/.bashrc"
  # Change the PATH right now:
  PATH="$PATH:$HOME/.local/bin"
fi
Jack G
  • 241