![]() ![]() Here’s the value of PATH variable for my system: echo $PATH Why can you not use the script name when you are in the same directory? That is because your Linux systems looks for the executables to run in a few selected directories that are specified in the PATH variable. before the script is important (when you are in the same directory as the script) If you are not in the same directory as your script, you can specify it the absolute or relative path to the script: Running Shell Script In Other Directory That. Most often you are in the same directory so you just use it like this. Once your script is executable, all you need to do is to type the file name along with its absolute or relative path. You can use the chmod command to give yourself this permission like this: chmod u+x script.sh So first you need to make sure that your script has the execute permission. Otherwise, you’ll have “permission denied” error when you try to execute the script. But for that to be possible, your file must be executable. The other method to run a shell script is by providing its path. If you have other shells installed, you can use that as well instead of bash.įor example, I installed zsh and used it to run the same script: Execute Shell Script With Zsh Method 2: Execute shell script by specifying its path There are other such shells program like ksh, zsh, etc. Shell is just a program and bash is an implementation of that. ![]() In this approach, you explicitly specified that you want to use bash as the interpreter for the script. If you use a normal text file, it will complain about incorrect commands. Keep in mind that it needs to be a shell script that you pass as argument. If you are not familiar already, I advise you to read my detailed guide on file permission in Linux. The first method involves passing the script file name as an argument to the shell.Ĭonsidering that bash is the default shell, you can run a script like this: bash hello.shĭo you know the advantage of this approach? Your script doesn’t need to have the execute permission. I am going to use this one line shell script to make things as uncomplicated as possible: cat hello.shĮcho "Hello World!" Method 1: Running a shell script by passing the file as argument to shell Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script. That maybe simple, but it doesn’t explain a lot. Or you can execute the shell script like this. There are two ways to run a shell script in Linux. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |