简体   繁体   中英

what does prefixed exclamation mark (`!`) command mean/do in shell?

We usually use $ as prompt in code block to demonstrate shell commands:

$ echo "hello world" > foo.md
$ cat foo.md
hello world

The problem is that you can't copy and paste those two lines with prefixed $ into your Terminal and execute them.

bash: $: command not found

However, I accidentally found that you can use prefixed ! for your commands (and prefixed # for outputs as comments)

! echo "hello world" > foo.md
! cat foo.md
# hello world
! ls

Now you can copy and paste above 4 lines into your shell and execute them at once.

Is that true? Is it worth to use ! for demonstration within code blocks in markdown files?

What does prefixed exclamation mark ( ! ) command mean/do in shell?

As @chepner mentions below, the standalone ! is acting as a logical not operator.

Beyond that, traditionally the $ prefix indicates that the command should be run as an unprivileged user and the # prefix indicates that the command should run as a privileged user (root). See this answer for some more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM