0

I read "Linux Bible 10th Edition", chapter 3: Using the shell, at 70 page. There is Tip section:

If you want to add your own commands or shell scripts, place them in the bin directory in your home directory (such as /home/cn/bin for the user named cn). This directory is automatically added to your path in some Linux systems, although you may need to create that directory or add it to your PATH on other Linux systems. So, as long as you add the command to your bin with execute permission, you can begin using it by simply typing the command name at your shell prompt. To make commands available to all users, add them to /usr/local/bin

I'm running Fedora Linux 35 Workstation Edition and trying to create my custom command. I've created ~/bin/kek.sh

#! /usr/bin/bash
echo "Kek"

and go to the command line, enter kek in my home directory. I got bash: kek: command not found.... After that I went to my ~/.bashrc and added following lines at the end of file

PATH=$PATH:~/home/bin/kek; export PATH;

Then returned to ~ directory, entered kek and got the same bash: kek: command not found.... I've changed .bashrc PATH line to PATH=$PATH:~/home/bin; export PATH;, reload terminal session and always getting the same result.

Please, explain, why it didn't work for me? How can I create my own command and make it accessible just by it's name? Thank you.

2
  • 1
    ~ refers to your home directory. To add your bin subdirectory to the path you should use PATH=~/bin:$PATH.
    – doneal24
    Mar 25 at 17:48
  • You created an executable file named kek.sh, however you are invoking kek, in UX style OS's, files are identified by its magic number not by extension, so if you want to call that file there are three steps involved. 1.- Ensure that your PATH environment variable includes the PATH where your executable is located. 2.- Check that your file is executable, at least for the person that is executing (700 ~ rwx------, here read write execute for the owner) 3.- Invoke it with the name that was saved in the filesystem (respect upper and lower case). that surely will do the job. Mar 25 at 18:35

1 Answer 1

5

The PATH variable indicates to your shell where executables may exist. So if you are placing your script kek.sh under ~/bin, then you would want to add ~/bin to PATH. However it appears that you have added ~/home/bin/kek to your PATH instead.

Another problem is that the script is named kek.sh, however the command you are trying to execute is kek. Either you need to change the name of kek.sh to kek or you need to be running kek.sh as your command.

Putting #!/usr/bin/bash at the top of your script (known as a shebang) indicates to the system what interpreter should be used on the script. Naming it with a .sh at the end does not affect how the shell executes the script as far as I know, so simply naming it kek would not change how it is executed so long as the proper interpreter is in place after #!

2
  • So, it's the right way to append the whole ~/bin directory to $PATH and not specific command?
    – EugZ
    Mar 25 at 17:51
  • 1
    Yes, you want to append the directory in which the commands you would like to execute exist and make sure that they have the execute bit set. You can echo $PATH to see how other directories have been set to see an example.
    – Natolio
    Mar 25 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.