I currently have a Unix script (written in bash) that executes several commands to modify a group of files, which I specify with an flist containing their paths. The general idea of the script is to create a directory, place files from the flist in that directory, and then perform the subsequent processes, a few of which are creating more directories.

The issue that I have with this is that I must specify the exact file path of where I want the directories to be located within the script itself. So, my question is: is there a way to write the script such that I can enter the command and then the path of the destination folder of where I would like the script executed on the command line and have the initial and all subsequent directories created by the script within the initial directory without having to supply any of their paths (i.e. 'command /destination/path/for/script/')?

Apologies if this is a bit long-winded. I’m quite new to Unix and have very little experience (and consequently could not come up with better wording). Any assistance is appreciated!

2 Answers 2


If I understand you correctly, you are asking how to pass values to a bash script. This is very easy, for example:

#!/usr/bin/env bash
echo "Directory is $directory"

$1 is the first command line argument of a bash script. $2 is the second etc etc. So, you could run the script above like so:

./foo.sh /path/to/bar
Directory is /path/to/bar

If you want the command to be a variable as well, you could do something like this:

 #!/usr/bin/env bash
 $command $directory

So, to run ls /etc, you would run the script above like this:

./foo.sh ls /etc
  • +1 for presenting a portable solution (using env).
    – Hennes
    Apr 25, 2013 at 18:54
  • This is exactly what I was looking for. Thanks!!
    – David
    Apr 25, 2013 at 19:58

I imagine that you have files in a place (/path/to/originals) and want to copy them to a target location (/path/to/destination) and modify them afterwards. Your current script looks like:

mkdir /path/to/destination
cp /originals/this-file /path/to/destination
cp /originals/this-other-file /path/to/destination
modify-somehow /path/to/destination/this-file
modify-somehow /path/to/destination/this-other-file

but you don't like to have to hardcode /path/to/destination everywhere. So you can ask to use "the value of the first positional parameter" instead of hardcoding /path/to/destination. As others mentioned, the value of the first positional parameter is $1.

So your script should be:

mkdir $1
cp /originals/this-file $1
cp /originals/this-other-file $1
modify-somehow $1/this-file
modify-somehow $1/this-other-file

And you should invoke this by adding the destination path as an argument:

my-script /path/to/destination

I tried to keep the script simple, but you could improve it, like using a single cp command to copy several files. You can also use a variable for your /originals path (but instead of an argument, this one sounds like a constant declaration at the beginning of your script)

Lastly, consider that if your filenames have spaces, you'll need to surround your $1 in double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.