When you use bash -c
, the first argument after the -c
is treated as a command, and any additional arguments are passed as positional parameters.
In your first fragment,
{"bash", "-c", "cd " + folder + "&& find . -type f | grep :*.txt "}
you are passing a single concatenated string argument "cd " + folder + "&& find . -type f | grep :*.txt "
whereas in the second fragment,
{"bash", "-c", "~/Final-Year/src/query", "&& ./checkuser.sh", user}
you are passing 3 arguments:
~/Final-Year/src/query
(which presumably should have been cd ~/Final-Year/src/query
&& ./checkuser.sh
user
so it's equivalent to
bash -c '~/Final-Year/src/query' '&& ./checkuser.sh' user
which executes bash -c '~/Final-Year/src/query'
or (corrected) bash -c 'cd ~/Final-Year/src/query'
with literal string && ./checkuser.sh
as positional parameter $0
and the substituted Java variable user
as positional parameter $1
What you likely want is
bash -c 'cd ~/Final-Year/src/query && ./checkuser.sh user'
which would instead be
{"bash", "-c", "cd ~/Final-Year/src/query && ./checkuser.sh " + user}
Alternatively, you could make use of the positional parameter array like
{"bash", "-c", "cd ~/Final-Year/src/query && ./checkuser.sh \"$1\"", "bash", user}
where the shell's name bash
is passed as $0
(you could use any arbitrary string here) and Java variable user
as $1
.