Search code examples
bashhead

Head command deletes part of line


I have a csv file (settings.csv) with the following data (separator is a space):

-n $(output_name) 
--outdir “peaks/$(output_name)” 
-g hs 
-f AUTO 
--qvalue 0.05 
--extsize 200 
-B
sample input output_name
sample-chip1 sample-input1 sample1
sample-chip2 sample-input2 sample2

When I call head -7 settings.csv I get this expected output:

-n $(output_name) 
--outdir “peaks/$(output_name)” 
-g hs 
-f AUTO 
--qvalue 0.05 
--extsize 200 
-B

Next I tried to store this output in a variable called settings with settings=$(head -7 settings.csv) and when I echo $settings I get this output:

$(output_name) --outdir “peaks/$(output_name)” -g hs -f AUTO --qvalue 0.05 --extsize 200 -B

It has deleted the -n part of the first line in the settings.csv, and I don't know why. How can I create $settings without losing -n?


Solution

  • The settings variable already contains the -n option you expect to be there. It’s just that the echo command interprets it as its own (echo’s) option.

    One way to avoid this problem is to quote the argument:

    echo "$settings"
    

    Alternatively, you can use

    printf '%s\n' "$settings"