Bash bad substitution with subshell and substring

前端 未结 6 772
旧巷少年郎
旧巷少年郎 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:11

    ${string:0:1},string must be a variable name

    for example:

    FOO="/foo/bar/baz"

    baz="foo"

    BAZ=eval echo '${'"$(basename $FOO)"':0:1}'

    echo $BAZ

    the result is 'f'

提交回复
热议问题