0

I am new to Linux. On my Desktop, I have created the following script called trimmomatic

#!/bin/bash
java -jar /home/aishah/software_library/Trimmomatic-0.36/trimmomatic-0.36.jar

When I open a terminal and do

$ cd Desktop

followed by

$ bash trimmomatic

the script works.

However, the script does not work if I run it starting from directories other than Desktop. For example, when I open a terminal and directly do

$ bash trimmomatic

it says

bash: trimmomatic: No such file or directory

What can I do to be able to run the script from any directory?

Please give me detailed instructions of all commands to type and where to type.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497

2 Answers2

3

There's couple things you need to keep in mind:

  1. bash accepts a script file as argument, which is what bash trimmomatic does, however bash trimmomatic implies that the file trimmomatic has to be in the current working directory where you execute the command bash trimmomatic. Command-line parameters which are files, unless specified with full path, have to be in current working directory.

  2. When you execute any non-builtin command in shell, such as ls or df, the shell goes through list of directories in $PATH variable. If a file is found and is executable in any of those directories, then the shell will execute it. Of course, ~/Desktop is not on the list of directories in $PATH, so you cannot call it directly as trimmomatic

Now, you have a single-line script. Practically speaking, a full script is unnecessary in case where you have just a single command there, unless it is intended to be shared with other users on the system. A far more practical way, would be to define an alias or a function in your ~/.bashrc file. For a function, you can do

trimmomatic(){
    java -jar /home/aishah/software_library/Trimmomatic-0.36/trimmomatic-0.36.jar
}

After you append this to ~/.bashrc, run source ~/.bashrc command, and the function can be called as

$ trimmomatic

Functions don't depend on $PATH, hence you can execute them anywhere. If you do insist on using a script, then I would suggest placing the script into ~/bin directory or ~/.local/bin. On Ubuntu ~/bin is added to $PATH when you sign in through ~/.profile config file, however ~/.local/bin is not, so you would have to add the following line to your ~/.bashrc file: PATH="$PATH:~/.local/bin. In both cases remember to source config file to update your current shell environment. Once that is done, the shell will start looking in either of those directories, and if your script is located there, it will be executed. Of course, this way you don't need to call the script via bash trimmomatic, rather you can call trimmomatic directly.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
0

Put the script in /usr/bin or in /bin and give it execution permission. Doing like this you should be able to execute the script from anywhere.