Skip to main content
63 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 '...
steeldriver's user avatar
  • 83.8k
52 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 ...
pLumo's user avatar
  • 23.2k
45 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 ...
Kusalananda's user avatar
  • 356k
44 votes

Is there any reason why I get ^[[A when I press up arrow at the console login screen?

TLDR You're probably running sh which outputs the raw keycodes generated when you press the Up arrow key. A more advanced shell like bash intercepts these keycodes and does something with them. E.g. ...
Snowcrash's user avatar
  • 789
30 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 ...
Marius Gedminas's user avatar
29 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 ...
Michael Homer's user avatar
25 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 ...
Gilles 'SO- stop being evil''s user avatar
20 votes
Accepted

Why do `ctrl+[` and `ESC` both produce `^[`?

This looks to follow the same logic as Ctrl-A, or ^A being character code 1, and ^@ being used to represent the NUL byte. Here, the ^ is a common way of representing Ctrl with another key. Namely, ...
ilkkachu's user avatar
  • 148k
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 ...
Stéphane Chazelas's user avatar
19 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, ...
Gilles 'SO- stop being evil''s user avatar
16 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 ...
ilkkachu's user avatar
  • 148k
15 votes
Accepted

Why does the special character `?` need to be escaped in grep, but not `.` or `*`?

This is simply the syntax specification as used by grep. Without any additional flag, (GNU) grep uses Basic Regular Expressions, which require some metacharacters (in its pure form(1), braces) to be ...
AdminBee's user avatar
  • 23.6k
14 votes
Accepted

Bad substitution: no closing "`" in a heredoc / EOF

The backtick introduces a command substitution. Since the here-document is not quoted, this will be interpreted by the shell. The shell complains since the command substitution has no ending backtick....
Kusalananda's user avatar
  • 356k
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}"
Stephen Kitt's user avatar
13 votes

Bash for loop with string var containing spaces

If you're using bash you can use an array for this #!/bin/bash files=('foo bar' 'another file' file1 'file2') for f in "${files[@]}"; do file -- "$f"; done Quoting is required for ...
Chris Davies's user avatar
13 votes
Accepted

Why does `htop` display `$'\t'` as `?` in `sort` command?

There’s nothing wrong with your command, htop replaces control characters with question marks: (((unsigned char)data_c[j]) >= 32 ? ((unsigned char)data_c[j]) : '?') (characters with values less ...
Stephen Kitt's user avatar
12 votes
Accepted

Why is a single backslash shown when using quotes

Section 3.1.2.3 Double Quotes of the GNU Bash manual says: The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within ...
Eliah Kagan's user avatar
  • 4,205
12 votes
Accepted

Where is the character escape sequence `\033[\061m` documented to mean bold?

\61 is the octal code for the 1 character in ASCII, so \e[\61m or \33[\61m or \33\133\61\155 or \33[1m is <ESC>[1m. That's CSI 1 m. See Wikipedia or the xterm documentation. $ printf '\e[\61m' ...
Stéphane Chazelas's user avatar
11 votes
Accepted

The easiest way to clear scrollback buffer of terminal + some deeper explanation?

From the man bash's readline section: clear-display (M-C-l) Clear the screen and, if possible, the terminal's scrollback buffer, then redraw the current line, leaving the ...
Electric Vibes's user avatar
11 votes

How to delete buffered text written to terminal during script execution

No. You can use Ctrlu to cancel the input buffer so that you don't accidentally enter it as a command with Enter. But you cannot remove text that's been pasted into a line above the current one. Yes. ...
Chris Davies's user avatar
10 votes

What is the difference between a byte and a character (at least *nixwise)?

POSIXly, emphasis mine: 3.87 Character A sequence of one or more bytes representing a single graphic symbol or control code. In practice, the exact meaning depends on the locale in effect, e.g. ...
ilkkachu's user avatar
  • 148k
10 votes
Accepted

How to run a loop inside sh -c

The usual wisdom is to define the script (after the -c) inside single quotes. The other part you need to use is a shell where the {1..4} construct is valid: $ bash -c 'for i in {1..4}; do echo $i; ...
QuartzCristal's user avatar
9 votes

What does `[$'\r\n']` mean?

This command removes all carriage return and line feed bytes from the line variable. This is a Bash (and zsh, and ksh) extension to parameter expansion: ${line//XYZ} replaces all matches of the ...
Michael Homer's user avatar
9 votes

awk print apostrophe/single quote

Note that you don't need awk for that. You can use the paste basic utility instead: < input paste -d "'" /dev/null - /dev/null It would be more efficient and have fewer limitations than ...
Stéphane Chazelas's user avatar
9 votes
Accepted

How can I find out what the escape codes my terminal are sending for certain special ones that cat will not show?

Simply use this command: showkey -a
egmont's user avatar
  • 6,518
8 votes
Accepted

Bash convert \xC3\x89 to É?

Hexdecimal numeric constants are usually represented with 0x prefix.Character and string constants may express character codes in hexadecimal with the prefix \x followed by two hex digits. echo -ne '\...
RomanPerekhrest's user avatar
8 votes
Accepted

Remove all type of special characters in unix .csv file

I'm not sure exactly what you mean by "special characters", so I'm going to assume that you want to get rid of non-ASCII characters. There are a few different tools that might work for you. The first ...
igal's user avatar
  • 10.2k
8 votes

scp to remote Windows hosts with spaces in path: ambiguous target

As mentioned in the question and answered here, method 2 was the solution that worked in my case. Adding as an answer for convenience of later readers. Windows 10 Git bash scp Single / Double Quotes: ...
s3dev's user avatar
  • 215

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