This question already has answers here:
Replace one character with another in Bash
                                
                                    (6个答案)
                                
                        
                                3个月前关闭。
            
                    
read -p "enter some words :" $PASTE

curl -s "example.com/${PASTE} | code continues


我想做的是,如果用户在PASTE变量中输入两个或两个以上我想用-代替空格的单词。

例:

PASTE=default application


新贴:

PASTE=default-application


我怎样才能做到这一点?

最佳答案

参见下文,使用'/'修饰符将''替换为'-'。注意“ read”命令中的拼写错误修复:

    # Notice no '$' for PASTE.
read -p "enter some words :" PASTE
    # Replace ALL ' ' with '-'
PASTE=${PASTE// /-}
curl -s "website.com/${PASTE} | code continues

关于linux - 在shell变量中用-替换空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59434460/

10-10 17:52