106 votes
Accepted

Is Vim immune to copy-paste attack?

Short answer: In many situations, Vim is vulnerable to this kind of attack (when pasting text in Insert mode). Proof of concept Using the linked article as a starting point, I was able to quickly ...
user avatar
45 votes
Accepted

How do some tools (e.g. nano , less) manage to leave no content in terminals after exit?

There are two worldviews here: As far as programs using termcap/terminfo are concerned, your terminal potentially has two modes: cursor addressing mode and scrolling mode. The latter is the normal ...
user avatar
  • 62.3k
40 votes
Accepted

How to use less in a script without getting ESC escape characters?

Use -R flag: -r or --raw-control-chars Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (...
user avatar
  • 11.9k
40 votes
Accepted

Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"

Use printf in its normal form: printf '%s\n' "${TEST}" From man printf: SYNOPSIS printf FORMAT [ARGUMENT]... You should never pass a variable to the FORMAT string as it may lead to errors and ...
user avatar
  • 20.7k
40 votes
Accepted

How do you output a multi-line string that includes slashes and other special characters?

In this case, I'd use cat with a (quoted) here-document: cat <<'END_CAT' /\_/\ ( o.o ) > ^ < END_CAT This is the best way of ensuring the ASCII art is outputted the way it is intended ...
user avatar
  • 12.9k
35 votes

How to escape quotes in shell?

Simple example of escaping quotes in shell: $ echo 'abc'\''abc' abc'abc $ echo "abc"\""abc" abc"abc It's done by finishing already opened one ('), placing escaped one (\'), then opening another one (...
user avatar
  • 18.3k
31 votes
Accepted

Is it possible to use ANSI color escape codes in Bash here-documents?

In your script, these assignments normal='\e[0m' yellow='\e[33m' put those characters literally into the variables, i.e., \e[0m, rather than the escape sequence. You can construct an escape ...
user avatar
29 votes
Accepted

How do I use grep while keeping the original text color?

You could do this, x | grep --color=never hello To quickly test it, you can do, ls -l /etc/ --color=always | grep --color=never .
user avatar
  • 50.9k
28 votes
Accepted

Weird symbols on screen when using grep?

The screenshot appears to show mangled ANSI colour codes, which control text rendering. Bold/bright text is produced with the sequence ␛[1m, which is usually interpreted by your terminal and ...
user avatar
28 votes
Accepted

awk print apostrophe/single quote

Here are a couple of ways: use octal escape sequences. On ASCII-based systems where ' is encoded as byte 39 (octal 047), that would be: awk '{print "\047" $0 "\047"}' input '...
user avatar
  • 72.4k
27 votes
Accepted

Why do I have to escape a "dot" twice?

Generally, you only have to escape one time to make special character considered literal. Sometime you have to do it twice, because your pattern is used by more than one program. Let's discuss your ...
user avatar
  • 144k
25 votes
Accepted

Caret square bracket square bracket A ^[[A - What does it mean?

These are ANSI escape codes. The ^[ represents an ESC (escape) character, the next [ is an actual left square bracket, and the letter indicates the function of the escape code. The Esc[ part is called ...
user avatar
  • 6,883
23 votes
Accepted

What does the ^ character mean in sequences like ^X^I?

^c is a common notation for Ctrl+c where c is a (uppercase) letter or one of @[\]^_. It designates the corresponding control character. The correspondence is that the numeric code of the control ...
user avatar
22 votes
Accepted

How to prevent random console output from breaking the terminal?

No: there is no standard way to "disable it", and the details of breakage are actually terminal-specific, but there are some commonly-implemented features for which you can get misbehavior. For ...
user avatar
20 votes
Accepted

How to escape single quotes correctly creating an alias

To make this an alias, which is possible, you need to use double quotes around the entire value for the alias. You'll also need to escape a few things within the alias as a result, and you need to ...
user avatar
  • 346k
19 votes
Accepted

Make Less highlight search patterns instead of italicizing them

Found an answer over on the superuser: https://superuser.com/questions/566082/less-doesnt-highlight-search Looks like it has to do with your TERM setting. For example, less highlighting acts normally ...
user avatar
  • 306
19 votes
Accepted

Which shells support ANSI-C quoting? e.g. $'string'

$'…' is a ksh93 feature that is also present in zsh, bash, mksh, FreeBSD sh and in some builds of BusyBox sh (BusyBox ash built with ENABLE_ASH_BASH_COMPAT). It isn't present in the POSIX sh language ...
user avatar
19 votes
Accepted

Escape a mm/dd/YY backup date in a file name

You can't have / (byte 0x2F on ASCII-based systems) in a file name, period. You can use characters that look like / like ∕ (U+2215 division slash) or ⁄ (U+2044 fraction slash though found in fewer of ...
user avatar
18 votes

Using/reading/writing the null and escape characters

Printing the null character On many recent shells you can write null character with dollar single quotes format $'\0', hexadecimal format \x00, unicode format \u0000 or \U00000000, or just as you ...
user avatar
  • 43.4k
17 votes

Removing control chars (including console codes / colours) from script output

There's an ansi2txt command in the colorized-logs package on Ubuntu. It removes ANSI color codes nicely, but it doesn't deal with things like progress bars produced by emitting ^H or ^M characters to ...
user avatar
17 votes
Accepted

Meaning of \[\e]0; in PS1 in .bashrc

The \e]0; is an escape sequence; \e is replaced with ASCII 27 (ESC), so the terminal receives the 4 characters ESC ] 0 ; tells xterm to set icon and title bar, that ends in BEL (\a). So the sequence ...
user avatar
16 votes

awk print apostrophe/single quote

In awk code, just put the apostrophe inside double quotes. You can concatenate strings by putting them next to each other (juxtaposition), no matter how these strings are constructed (literal, ...
user avatar
15 votes

How do you output a multi-line string that includes slashes and other special characters?

Quote it with single '' or double quotes "" to prevent the <>() from being taken as operators. Then if you want it on one line, use echo -e or $' ' in Bash, mark the newlines as \n and ...
user avatar
  • 116k
14 votes

Make Less highlight search patterns instead of italicizing them

The mention of LESS_TERMCAP_so was incomplete. That is less's special environment variable used to override the termcap so (standout) capability. To use this capability, you have to provide a se (...
user avatar
14 votes
Accepted

Hide and unhide cursor with tput

In the ncurses terminal database, cvvis is used as documented in the terminfo manual page: cursor_visible cvvis vs make cursor very visible ...
user avatar
14 votes
Accepted

What is the gnome-terminal ANSI escape sequence for "CTRL + arrow/s"?

Gnome-terminal (more properly VTE) imitates some version of xterm's escape sequences. How closely it does this, depends on the version of VTE. The relevant xterm documentation is in the PC-Style ...
user avatar
13 votes

How to determine the current color of the console output?

In an xterm you can get the current color RGB color codes reported by altering a color change escape to a query. Use the ESC ] Ps m - but add a ? question mark. From the docs: If a "?" is given ...
user avatar
  • 55.7k
13 votes

Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"

You should never put variable content in the format string given to printf. Use this instead: printf '%s\n' "${TEST}"
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible