-1

I tried to use FreeBSD. The FreeBSD root user uses csh by default.

user@freebsd-13:~ $ echo $SHELL
/bin/csh

I know that setting a variable via $() does not work in csh.

root@freebsd-13:~ # export test=$(echo hello3)
Illegal variable name.

I want to do something like that

root@freebsd-13:~ # sh -c "export test=$(echo hello3)"
Illegal variable name.
root@freebsd-13:~ #

And it does not work either... However, this works:

root@freebsd-13:~ # sh -c "echo "hello""
hello

Or this works as well but in the scope of sh:

root@freebsd-13:~ # sh
# export test=$(echo hello3)
# echo $test
hello3
# exit
root@freebsd-13:~ # echo $test
test: Undefined variable.
root@freebsd-13:~ #

Another attempt to set a variable via sh:

root@freebsd-13:~ # sh -c "export test=`echo hello5`"
root@freebsd-13:~ # echo $test
test: Undefined variable.
root@freebsd-13:~ # sh -c "echo "$test""
test: Undefined variable.
root@freebsd-13:~ #
root@freebsd-13:~ # /bin/sh -c "export test=`echo hello3`"
root@freebsd-13:~ # echo $test
test: Undefined variable.
root@freebsd-13:~ # /bin/sh -c "echo "$test""
test: Undefined variable.
root@freebsd-13:~ #

Tried to source it:

root@freebsd-13:~ # . sh -c "export test=$(echo hello3)"
Illegal variable name.
root@freebsd-13:~ #
root@freebsd-13:~ # . sh -c "export test=`echo hello3`"
.: Command not found.
root@freebsd-13:~ #
root@freebsd-13:~ # source sh -c "export test=`echo hello3`"
sh: No such file or directory.
root@freebsd-13:~ #
root@freebsd-13:~ # source /bin/sh -c "export test=`echo hello3`"
Unmatched '"'.
root@freebsd-13:~ # source /bin/sh -c "export test=$(echo hello3)"
Illegal variable name.
root@freebsd-13:~ #
root@freebsd-13:~ # source /bin/sh -c "export test="$(echo hello3)""
Illegal variable name.
root@freebsd-13:~ #

How should it be done properly?

1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Jun 19 at 11:46

1 Answer 1

0

Ok, I'm answering my question.
First of all the source command is used with files only.
If I want to run the code the way I described, I need to use eval.
Got a very clear explanation here:
How to source some shell code not a file?

The crucial point for my understanding was that shebang line is ignored while sourcing, since the file isn't being executed: only its contents are.

So, the problem here is that to export a variable I need to run it in the current shell. And this current shell ignores any shebangs while sourcing, moreover, if I run sh -c - I run the code in a subshell.

csh does not support sh syntax. So, the only possible way to export a variable while using csh is to use its syntax.

root@freebsd-13:~ # setenv test `echo "hello world"`
root@freebsd-13:~ # echo $test
hello world

There are some other workarounds which are similar to those described here: https://stackoverflow.com/questions/2710790/how-to-source-a-csh-script-in-bash-to-set-the-environment

(but I need vice versa - source a bash script in a csh environment)

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.