0

In Ubuntu if I type which more, it will display the path of the more command as /bin/more, indicating it is a file. But I don't see any extension for it nor a type associated, probably because they are simple executables. How can I create such a file which could echo a simple string?
I don't want to create a .sh file because if I want to pass the file as a command with another command, I'd have to type .sh as well.
TIA

  • 2
    I think this answers your question http://askubuntu.com/questions/427818/how-to-run-scripts-without-typing-the-full-path symlink your .sh to a command in /bin http://askubuntu.com/questions/427818/how-to-run-scripts-without-typing-the-full-path – Mark Kirby Aug 09 '16 at 11:21
  • @MarkKirby I already did that like a minute ago, isn't there a better way to do this? – Akash Agarwal Aug 09 '16 at 11:22
  • What better than running a single command? It gives you exactly what you want with almost no effort. – Mark Kirby Aug 09 '16 at 11:22
  • @MarkKirby I agree it gets the job done, but it feels like a 'hack' to me. I'm trying to explore how the files like more are created :) – Akash Agarwal Aug 09 '16 at 11:24
  • 2
    Or you just don't use the .sh extension for your script, and ensure it starts with a shebang. If the script is in your user's PATH, somewhere such as /usr/local/bin, just typing the script name will run it as a command. – Arronical Aug 09 '16 at 11:25
  • It is not a hack symlinks are part of Ubuntu, all it does is link the .sh with a command in /bin – Mark Kirby Aug 09 '16 at 11:25
  • @MarkKirby "feel like a `'hack'" for my exploration. – Akash Agarwal Aug 09 '16 at 11:29
  • @Arronical excellent idea, it works! – Akash Agarwal Aug 09 '16 at 11:29
  • "I'm trying to explore how the files like more are created" It is an executable file, read here http://askubuntu.com/questions/484718/how-to-make-a-file-executable – Mark Kirby Aug 09 '16 at 11:29
  • 1
    @MarkKirby Ah! True. I thought it was something similar to packages installed by apt-get. I got everything pretty confused. Thank you for clearing it out! – Akash Agarwal Aug 09 '16 at 11:31

2 Answers2

4

Un*x like OS are not interested in file extensions. To make a shell script executable, put

#!/bin/sh

as first line in the script to define the interpreter (or #!/bin/bash if you need bash extensions, or whatever shell you chose). And change the mode to executable:

chmod a+x <filename>

You have to provide the full or relative path to the executable, if it is not in a directory in your PATH environment.

Edit:

If there is a ./bin subdirectory in your home directory, this will usually be included in your $PATH when logging in (see the .profile script). So you may create $HOME/bin and put the executable file there

ridgy
  • 2,356
3

You do not need to use the .sh extension for your shell scripts. They should start with a shebang to signal that it's a shell script. #!/bin/bash is the shebang for a bash script, or if you'd like the OS to decide which shell to use, based on the default shell use #!/bin/sh.

To be able to execute the script as a command, just by typing the name, it should have; a shebang, executable permissions, and should be located in a directory which is included in your PATH environment variable.

You can see all of these locations by using:

echo $PATH

The output should be:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Once those three conditions have been met, your script will execute by just typing the name.

Arronical
  • 19,893