1
user="none"
noneexist="false"
id -u $user > /dev/null 2>&1
if [[ $? -eq 0]]; then ${user}exist="true"; else echo "$user do not exist"; fi
-bash: syntax error in conditional expression: unexpected token `;'
-bash: syntax error near `;'

I am receiving this error, not sure what is wrong with my statement.

duplode
  • 33,731
  • 7
  • 79
  • 150
Pradeep
  • 69
  • 1
  • 5

1 Answers1

2

Put a space before your ;

if [[ $? -eq 0 ]] ; then ${user}exist="true" ; else echo "$user do not exist" ; fi

You need a space on either side of your conditional brackets ([[ and ]]). All of the following will not work:

  • if [[$? -eq 0 ]] ; ...
  • if [[ $? -eq 0]] ; ...
  • if [[ $? -eq 0 ]]; ...
wcarhart
  • 2,685
  • 1
  • 23
  • 44
  • thank you. i edited to post and appreciate if you can point the mistake ? I am not sure if i have to edit or post a new question. – Pradeep Aug 28 '19 at 22:47
  • @Pradeep check out the question that was marked as duplicate. It states that you need spaces before and after `[[` and `]]`. I'll add some more detail to my answer. – wcarhart Aug 28 '19 at 22:51
  • @Pradeep if you think I've solved your question, you can accept my answer with the checkmark next to my answer. – wcarhart Aug 28 '19 at 22:53
  • 1
    @Pradeep, I see you're running into another issue. You won't be able to get an answer for that issue on this question thread; you'll have to open another one. However, this new question is already answered here: https://stackoverflow.com/a/9715739/6246128. There are many ways to do this, I prefer `read "$VARNAME" <<<"$VARVALUE"` – wcarhart Aug 28 '19 at 23:03
  • @Pradeep WRT the assignment problem, the shell doesn't really support dynamically-named variables (you can do it, but it's all kind of weird and klugy). I'd recommend backing up and asking if there's a better way of accomplishing the end goal. – Gordon Davisson Aug 29 '19 at 01:19