-1

I have to make my own command shell where a bash script calls the shell executable and does so with quotes. The problem is that when I try to do so with logical operators it seems that the quotes turn off the special characters &&. For example:

var=("echo hello && mkdir testing")
./rshell ${var}

The command shell ends up outputting hello && mkdir testing instead of actually following the logic of the operator where it successfully outputs hello and then makes directory testing. How can I fix this?

  • What do you mean by "command shell"? – Jesse_b May 11 at 13:23
0

This definitely seems like an x-y problem. It would help to know what the ultimate end goal for this is, but:

  • Your variable var is actually an array because of the var=( ... ) syntax
    • Because you quote everything in the parenthesis you are adding all data to a single element
    • You are not using the proper syntax to call an array (${var[@]}) which in this example isn't a big deal since everything is in a single element anyway

The biggest problem seems to be the way the logical AND gets quoted and interpreted. If you don't quote it when adding to the array the shell will attempt to evaluate it, and if you do quote it, it will remain a literal string preventing evaluation. One of the ways I was able to resolve this (short of using eval) is with the following:

script.sh

#!/bin/bash

bash -c "$(echo $@)"

$ tree
.
└── script.sh

0 directories, 1 file
$ cmd=(echo hello \&\& mkdir test)
$ ./script.sh "${cmd[@]}"
hello
$ tree
.
├── script.sh
└── test

1 directory, 1 file

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.