5

I'm reading a book on shell programming and learning that following commands are equivalent, which beep on my Mac but don't make any sound on Ubuntu:

$ echo $'\a'

$ echo -e "\a"

$

however in both cases, terminal prompts a blank line. My questions are:

  1. What's $'\a' here? Parameter expansion, command substitution or something else?
  2. Why echo prints empty line as if the parameter is undefined, like for instance, in the case of

$ echo $NONSENSE which prompts empty line? Thanks!

1 Answer 1

5

don't make any sound on Ubuntu:

perhaps because your particular terminal emulator is configured to avoid sounds, or because the pcspkr kernel module is unloaded, etc... You could use another terminal emulator (e.g. the old xterm) which should beep.

What's $'\a' here?

Read the chapter on shell expansion of the Bash manual. It is called ANSI-C quoting (as commented by South Parker).

Why echo prints empty line

the echo command (read more echo(1)...) is often a bash shell builtin so (without any -n) it prints its expanded arguments (here the bell character) followed by a newline. But your terminal emulator don't ring the audible bell (and the bell character is not displayed, since it is a control character)

BTW, Apple is rumored to dislike the GPLv3+ license, so you might upgrade your bash to a recent version (e.g. 4.4 in august 2017) on your Apple computer.

You might read the tty demystified for an historical approach to terminal emulators on Unix. See also pty(7).

4
  • That's intensively informative, huge thanks! :)
    – mzoz
    Commented Aug 22, 2017 at 7:43
  • 2
    $'...' isn't called "quote removal", but the bash manual doesn't give it a name. This form of quoting is also supported by at least the ksh93 shell but not by pdksh. I believe it's an extension to the POSIX syntax.
    – Kusalananda
    Commented Aug 22, 2017 at 8:37
  • 1
    Kusalananda I think you are right, the syntax is called ASCI-C quoting: gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
    – mzoz
    Commented Aug 22, 2017 at 14:30
  • 2
    @SouthParker Ah, that's the name of it. Thanks for helping me remember!
    – Kusalananda
    Commented Aug 22, 2017 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .