Bash bad substitution with subshell and substring

前端 未结 6 842
旧巷少年郎
旧巷少年郎 2021-02-12 10:26

A contrived example... given

FOO=\"/foo/bar/baz\"

this works (in bash)

BAR=$(basename $FOO) # result is BAR=\"baz\"
BAZ=${BAR         


        
6条回答
  •  野的像风
    2021-02-12 11:24

    As others have said, the first parameter of ${} needs to be a variable name. But you can use another subshell to approximate what you're trying to do.

    Instead of:

    BAZ=${$(basename $FOO):0:1} # result is bad substitution
    

    Use:

    BAZ=$(_TMP=$(basename $FOO); echo ${_TMP:0:1}) # this works
    

提交回复
热议问题