English 中文(简体)
如何防止bash在不使用引号或反斜杠的情况下处理参数?[已关闭]
原标题:How can I prevent bash processing an argument without using quotes or backslashes? [closed]
  • 时间:2023-06-24 23:15:49
  •  标签:
  • bash
  • quotes
Closed. This question needs to be more focused. It is not currently accepting answers.

想改进这个问题吗 通过编辑此文章更新问题,使其仅关注一个问题。

Closed 11 hours ago.

关键的问题是,我想调用一个参数包含特殊字符(esp:&;)的脚本,但在传递之前不让bash处理参数。在我的用例中,用引号或反斜杠修改参数是不方便的。

自从发帖以来,我自己找到了答案,那就是使用阅读,如下所示:

~$ read -p "enter a string:" str
enter a string: x&x&x&x&

~$ echo "the string: " "$str"
the string:  x&x&x&x&

在我看来,这是一个有用的问题,有着有用的解决办法。

原来的问题如下:


我编辑了这个以关注真正的问题。。。

鉴于

~$ function x () { echo  !  ;}

~$ x
!

~$ echo x
x

…以下命令创建后台进程:

~$ echo x&x&x&x&
[2] 27320
[3] 27321
[4] 27322
x
[5] 27323
[2]   Done                    echo x
!
!
sean@lyra:~$ !

[3]   Done                    x
[4]   Done                    x
[5]-  Done                    x

这让我很惊讶,因为我已经习惯于将参数作为字符串进行echo和printf处理,而不是执行它们。

这方面的规则是什么?为什么后台进程是默认执行的,而前台进程需要显式调用?

例如:

~$ echo $(x)
!

除了引用参数之外,还有什么方法可以绕过shell中的这种行为吗?

我希望这次编辑不会混淆。我在第一篇文章中并没有真正理解这个问题。谢谢Davis Herring。

对于上下文,我希望能够将url作为参数粘贴到函数中,而不必在其周围加引号。有人能建议一种方法,在不调用命令解释器的情况下从控制台读取此字符串并将其传递给函数吗?

Cheers, bitrat

问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签