0

I'm writing a bash script to format my partitions. One of the commands I want to run is the following:

mkfs.btrfs -f -m single -L root /dev/sda1

So I have split that command into the options part: -f -m single -L root and the partition part: /dev/sda1 and put them into variables like this:

mkfs.btrfs "${myoptions}" /dev/"${mypartition}"

but it fails with:

mkfs.btrfs: invalid option -- ´ ´

I have tried different variations of it like putting the 2 parts in doubles quotes but all fails. I have also tried to put the two parts into an array and run it like this:

mkfs.btrfs "${array[1]}" "${array[2]}"

or like this:

mkfs.btrfs "${array[*]}"

but it also fails.

If I run it without the options part it works, so my guess is that the issue comes from the empty space between the two arguments but I don't know how to solve it.

2
  • Can you specify how you have run the script on CLI ? It might just be that you have to put your arguments in quotes. Like : $ script "-f -m single -L root" "sda1" Commented May 1, 2018 at 15:10
  • It's apparently the spaces (all of them) like @Ignacio Vazquez-Abrams wrote. Commented May 2, 2018 at 14:46

2 Answers 2

3

I have also tried to put the two parts into an array...

Not good enough. Every single argument must be an element of an array (or the only value in the variable).

args1=(-f -m single -L root)
args2=/dev/"$mypartition"
mkfs.btrfs "${args1[@]}" "$args2"
0

This sort of thing is always a huge pain for me. In this case since none of your arguments have spaces themselves, I would try removing the double quotes and letting Bash break it into words for your $myoptions variable. So this:

mkfs.btrfs ${myoptions} /dev/"${mypartition}"

With the double quotes you get one field, -f -m single -L root containing spaces. Without it, it should be broken into the individual fields -f -m single -L and root and show the same behavior as typing mkfs.btrfs -f -m single -L root /dev/sda1 in the interactive shell.

If you do have some arguments that contain spaces, you might have to get tricky with the $IFS variable and your encoding.

You can also use the special variable $@ which has its own rules.

I write all my shell stuff to POSIX though so I'm sure there's an easier way to do this in Bash.

2
  • 1
    I tried this: mkfs.btrfs ${myoptions} /dev/"${mypartition}" again but it didn't work. I took a look at this special variable $@ but if I'm not wrong it's for functions you define yourself and not given functions. Commented May 2, 2018 at 14:51
  • No you're right, I thought removing the quotes would work in this specific case and be simple. $@ is a special parameter you can mimic the behavior of an array with because sh doesn't have an array. Honestly, I write my scripts to be sh compatible because I enjoy it and it's supported in all POSIX compatible shells, including Bash. If you're interested just for fun, etalabs.net/sh_tricks.html is a short, well written and presented list of some of the more useful tricks (including using $@). Commented May 3, 2018 at 1:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.