0

Team, any hint? unable to export file contents as var in bash. I am expecting all the below list is add to the var with a space.

# cat /tmp/svc
src/cmd
src/common
src/cue
src/pipelines
src/resources
src/services
src/tests
src/ui

# export testvar=$(cat /tmp/svc)

/bin/sh: 41: export: src/common: bad variable name

expected

testvar="src/cmd src/common src/cue...."

AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

0

I was missign quotes

export testvar="$(cat /tmp/svc)"
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • Note that `testvar=$(cat /tmp/svc); export testvar` would work just as well without quotes being needed. It would work in the other direction too: `export testvar; testvar=$(cat /tmp/svc)`. This is because word-splitting (which quotes are needed to avoid in a regular command) is suppressed when using the shell's assignment syntax; but `export` doesn't act like an assignment to the parser -- it's a builtin, but from the parser's perspective it acts like other commands. – Charles Duffy Jan 24 '23 at 01:23
  • 1
    @jhnc I see this error in dash, but it may be version-dependent. According to Gilles' answer [here](https://unix.stackexchange.com/questions/68694#68748), "Note that you do need the double quotes after export or readonly, because in a few shells, they are still ordinary builtins, not a keyword. This is only true in some shells such as some older versions of dash, older versions of zsh (in sh emulation), yash, or posh; in bash, ksh, newer versions of dash and zsh export / readonly and co are treated specially as dual builtin / keyword (under some conditions) as POSIX now more clearly requires." – Gordon Davisson Jan 24 '23 at 01:57