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
- 260
2 Answers
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
- 2,516
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.
- 20,241