3

I have made some scripts that I would like to call in a terminal with just a single word (like the other traditional shell command, cd, ls, etc.).

So far, I found that I can put the scripts into one of the directories pointed to by the environment path (bin directories) or to add the script's directory to the environment path.

Apparently, Ubuntu recommends not to modify any bashrc files. Moreover, the bashrc files appear to call only bash_aliases which I guess, is more for aliases. Moreover, I found that I could change the path for all user in /etc/environment or making a script in /etc/profile.d.

So what I would like to know is what is the best practice to create a custom command accessible to one user only and to all the users? (Use the bin directories or modify the PATH? If the latter, which file modify?)

Eliah Kagan
  • 117,780
F.Jean
  • 41
  • 1
  • 3
  • 2
    i can't speak to best-practices ... but I was told in the 80s to put all (unix) [single] user scripts in ~/bin... as for global scripts; /usr/local/bin unless there is a standard where I'm working. the reason you don't want to modify bashrc files is to allow updates to change them without wiping your changes (or giving you lots of questions if updating from terminal). maybe modify .profile if you need to add ~/bin.. – guiverc Sep 13 '17 at 13:08
  • 4
    "Apparently Ubuntu recommend not to modify any bashrc files" I don't think this is true (to an extent). aliases nowadays go to bash_aliases but it is still custom to set PATH for USERS in bashrc. But that can also be done in ~/.profile if need be. Or in /etc/profile – Rinzwind Sep 13 '17 at 13:38
  • Effectively, Ubuntu has tutorial modifiying direcly the bashrc, my bad. But as guiverc said I read that update could wipe your bashrc file, so I didn't want to do it that way. – F.Jean Sep 13 '17 at 14:50

2 Answers2

8

So I would like to know the best practice to create a custom command accessible to all the users.

/usr/local/bin. See the Filesystem Hierarchy Standard Ubuntu/Linux follows.

4.9 /usr/local : Local hierarchy

4.9.1 Purpose

The /usr/local hierarchy is for use by the system administrator when installing software locally. It needs to be safe from being overwritten when the system software is updated. It may be used for programs and data that are shareable amongst a group of hosts, but not found in /usr.

Locally installed software must be placed within /usr/local rather than /usr unless it is being installed to replace or upgrade software in /usr.

4.9.2 Requirements

The following directories, or symbolic links to directories, must be in /usr/local

"/usr/local"
bin
games
include
lib
man
sbin
share
src

So I would like to know the best practice to create a custom command accessible only to one user

mkdir $HOME/bin
gedit $HOME/.bashrc

and add at the bottom:

export PATH="$HOME/bin:$PATH"

If need be you can also set this in ~/.profile and in /etc/profile (there you will need to use the username to create specific PATHs though).

Example .profile:

if [ -d "$HOME/bin" ] ; then
  PATH="$PATH:$HOME/bin"
fi
Rinzwind
  • 299,756
  • 1
    That's the place for commands for all users. For only the current user, create a bin directory directly in your home directory (if it doesn't exist yet) and place the scripts in there. You need to restart the shell (close and reopen terminal window) after you create the directory, so that the system includes it in your $PATH. – Byte Commander Sep 13 '17 at 13:28
  • Correct but I was trying to find proof for "Apparently Ubuntu recommend not to modify any bashrc files" 1st :D – Rinzwind Sep 13 '17 at 13:36
  • 2
    But the section that adds ~/bin to the user's $PATH is already inside .profile by default. No need to edit anything. – Byte Commander Sep 13 '17 at 14:03
  • @ByteCommander That's true for $HOME/bin, but someone might want to add a different directory like $HOME/.bin, then the instructions come in handy. – dessert Sep 13 '17 at 18:48
  • @dessert If someone wants to put a "hidden" directory in their PATH, then they may edit their .bashrc or .bash_profile to add it themselves. If they need help doing this, they need to seriously reconsider why they're "coloring outside the lines" by using something other than $HOME/bin that's provided to them by default. – Monty Harder Sep 13 '17 at 22:10
3

I am answering the original question about a custom command (not only a custom script).

Simple command - alias

A simple command for one single user (the current user) can be created as an alias (you mentioned alias in your question), for example

alias rm='rm -i'

to make the remove command interactive. This works 'now' and only in the current terminal window. You can save it in ~/.bashrc near the standard aliases, and it will be activated in all terminal windows in the future and also the text screens.

Please notice that your current user's aliases do not work with sudo (unless you create an alias for the the user root too and store it in /root/.bashrc).

It is a good idea to backup the .bashrc file(s) before editing, just in case, for example

cd
cp -p .bashrc .bashrc.0

See also this link, Combine 2 commands into 1 Custom command?

Advanced command - function or shellscript

If you want to call a more advanced command, that consists of several command lines and/or uses parameters in a complicated way, you can use

  • a function, which is defined like the following examples

    • either like this (bash only)

      function rmi {
       rm -i
      }
      
    • or more general (also when the shell is invoked as sh)

      rmi () {
       rm -i
      }
      

    Such a function is activated like an alias, and can be stored in the same way in ~/.bashrc.

  • a shellscript, which is a text file with the bash commands.

    • If you want only the current user to use the shellscript, you can store it in ~/bin.

      mkdir ~/bin  # create `~/bin`, if it is not already created.
      
    • Otherwise, if you want all users to use the shellscript, you can store it in one of the general directories for executable programs and scripts, for example /usr/local/bin (or /usr/local/sbin if the script is to be run with sudo).

      Please check very carefully that the shellscript has a unique name, so that you do not 'compete' with (or even worse, overwrite) an already existing executable program.

sudodus
  • 46,324
  • 5
  • 88
  • 152