Bash bad substitution with subshell and substring

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

    Modified forms of parameter substitution such as ${parameter#word} can only modify a parameter, not an arbitrary word.

    In this case, you might pipe the output of basename to a dd command, like

    BAR=$(basename -- "$FOO" | dd bs=1 count=1 2>/dev/null)
    

    (If you want a higher count, increase count and not bs, otherwise you may get fewer bytes than requested.)

    In the general case, there is no way to do things like this in one assignment.

提交回复
热议问题