0

I usually combine options together whenever there are more than one option to be used with respect to some command. For example , if i want to create an archive using tar i will write tar -cvf archive.tar file1 file2 but my doubt is that how to know the correct order in which i have to combine the options together. If i use tar -cfv archive.tar file1 file2 it shows error. I have faced this issue with many other commands also. I know it is a very silly doubt but i was having a really hard time getting through it. I have checked the man description of the commands also but there they have specified a particular sequence under the synopsis section. I was not able to find something related to combining the options in a particular sequence.

2 Answers 2

2

The manual for any given command will describe exactly how to use its options.

In this case, the -f option takes a filename argument. An option's argument (if it takes one) must be placed just after it. In your first tar command, this filename argument is archive.tar, but in your second it is v.

The second command tries to create an archive called v from three files, archive.tar file1, and file2. Since archive.tar probably does not exist, you would get an error message about this.

Again, the tar manual would describe this. The GNU tar manual say

tar -c [-f ARCHIVE] [OPTIONS] [FILE...]

so it's clear that -f takes the name of an archive. A bit further down, it says

-f, --file=ARCHIVE

Use archive file or device ARCHIVE. [...]

The other options that you use, -c and -v, don't take arguments.

Also, in general, options come before file operands. Some GNU tools allow you to add opions to the very end of the command line, as in

tar -c -f archive.tar file1 file2 -v

but this is (IMHO) bad style, and it would break on many other Unices (-v would be interpreted as a file name).

The 100% correct way to write your tar command, following the form in the synopsis, is

tar -c -f archive.tar -v file1 file2
1
  • 1
    thank you so much for clear explanation
    – LocalHost
    Jul 30, 2019 at 6:35
0

The issue most usually has to do with options which require a following argument. Let's look at tar as an example.
-f requires a filename, so it must be followed by a filename.

A general form of argument parsing in pseudocode might help illustrate this: Remember that arguments are given to the command as a vector (-cvf file would be broken to argv[0]=c argv[1]=v argv[2]=f argv[3]=file)

While $argv is not empty
do
   case $argv[0] in # argv[0] is pointer to leftmost, or first, arg
       c) # single element argument
          set internal variable create_mode=true
          shift # remove argv[0], shifting all indexes down by 1, so argv[0] now points to former argv[1]
       f) # A double element argument
          set internal variable use_file=true
          shift
          set internal variable file_name=$argv[0] # We did a shift, so now argv[0] points to the argument following 'f'
          shift
       v) # Another single element argument 
          set internal variable verbose=true
          shift
      # and so on, there is a case for each possible argument, and shifts according to the number of elements in each argument
done 

The loop will continue processing each argument in argv, until argv (argument vector) is empty.
As you can see, if an argument has several parts, due to the implementation of argument processing, all it's parts must follow the argument immediately.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .