linux - 将 shell 变量中的空格替换为 -

标签 linux bash shell

read -p "enter some words :" $PASTE

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

我想要做的是,如果用户输入两个或多个单词,我想用 PASTE 中的 - 替换空格变量。

示例:

PASTE=default application

新粘贴:

PASTE=default-application

我怎样才能做到这一点?

最佳答案

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

    # 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/

相关文章:

linux - 删除包含多行字符串中单词的内容

java - 在 Gnome 桌面上打开 .jar 文件

linux - 需要增加HTTP并发连接数到85000

linux - 按第一个字符复制文件夹的 Bash 脚本

linux - 如何拆分变量以在行之间打印出其他内容?

Git post-commit hook 作为后台任务

linux - 未检测到 MC7455 sierra 无线 PCI

c - pthread_create 在可用内存不足的情况下因 ENOMEM 而失败

bash - 在 bash ls 别名中使用星号

linux - 在读取 bash 数组时(不进入)