This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to use command line arguments in bash?

Hi all,

As I'm relatively new to bash scripts, I would need some help if you don't mind, please.

I've written a bash script and it is working perfectly on test files. Now, I would like to use command line arguments so I can use this bash script on any other files I want.

For example, I have this line in my script with a test file:

cut -f 1 TEST_FILE.txt > headers.txt

I would like to be able to do something like this in my Linux terminal:

bash script.sh -i input_any_file.txt

And in my script:

cut -f 1 any_file.txt > headers.txt

So how can I use an input file in an argument and "call" this file on the script? If you have any suggestions, I would be happy to hear them ...

Thank you in advance!

shell bash

Hello audrey.durand!

We believe that this post does not fit the main topic of this site.

I';m afraid this is a straightforward programming question, not a bioinformatics question. It would be more appropriate on StackOverflow. That said, this has been answered thoroughly in many places. Search google for getopt tutorials. See for instance: Comparing reads of multiple BAM files (using getopts for parsing)

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

The simplest way is to use $1, $2, ... within the script to access the command line args. This would be without any fancy -i flags, though, just raw access of the arguments in order how they are supplied to bash script.sh.

0 answers

No answers yet.

Log in to answer this question.