在没有在Bash中扩展的情况下,将文字发送到文件 [英] Echoing a tilde to a file without expanding it in Bash

查看:114
本文介绍了在没有在Bash中扩展的情况下,将文字发送到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Bash脚本中的一个文件中写一个参数,所以我正在做这样的事情,

I need to write an argument to a file in a Bash script, so I'm doing something like this,

echo "Argument is: $1" >> file

问题是如果在我不想要的参数中有一个波浪号(〜)扩展到主目录。所以如果用户把〜/ bin作为一个参数传递给脚本,它会被写成〜/ bin而不是/ home / user / bin。我如何做到这一点?

The problem is that if there's a tilde (~) in the argument I don't want it expanded to the home directory. So if the user passed ~/bin as an argument to the script, it would get written as ~/bin and not /home/user/bin. How do I do this?

推荐答案

我假设你的程序是以

$ your_prog ~/foo

参数是在您的程序启动之前翻译过,所以除了教育用户不需要扩展的情况外,还可以避免这样做:除了不需要扩展外,您还可以执行以下操作:

The argument is translated before your program is even started, so there's nothing you can do to prevent it other than educating the user to quote appropriately if the expansion is not desired:

$ your_prog "~/foo"

这篇关于在没有在Bash中扩展的情况下,将文字发送到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆