Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

101 votes
16 answers
85918 views
Removing control chars (including console codes / colours) from script output
I can use the "script" command to record an interactive session at the command line. However, this includes all control characters *and* colour codes. I can remove control characters (like backspace) with "col -b", but I can't find a simple way to remove the colour codes. Note that I want to use the...
I can use the "script" command to record an interactive session at the command line. However, this includes all control characters *and* colour codes. I can remove control characters (like backspace) with "col -b", but I can't find a simple way to remove the colour codes. Note that I want to use the command line in the normal way, so don't want to disable colours there - I just want to remove them from the script output. Also, I know can play around and try find a regexp to fix things up, but I am hoping there is a simpler (and more reliable - what if there's a code I don't know about when I develop the regexp?) solution. To show the problem:
spl62 tmp: script
Script started, file is typescript
spl62 lepl: ls
add-licence.sed  build-example.sh  commit-test         push-docs.sh
add-licence.sh   build.sh          delete-licence.sed  setup.py
asn              build-test.sh     delete-licence.sh   src
build-doc.sh     clean             doc-src             test.ini
spl62 lepl: exit
Script done, file is typescript
spl62 tmp: cat -v typescript
Script started on Thu 09 Jun 2011 09:47:27 AM CLT
spl62 lepl: ls^M
^[[0m^[[00madd-licence.sed^[[0m  ^[[00;32mbuild-example.sh^[[0m  ^[[00mcommit-test^[[0m         ^[[00;32mpush-docs.sh^[[0m^M
^[[00;32madd-licence.sh^[[0m   ^[[00;32mbuild.sh^[[0m          ^[[00mdelete-licence.sed^[[0m  ^[[00msetup.py^[[0m^M
^[[01;34masn^[[0m              ^[[00;32mbuild-test.sh^[[0m     ^[[00;32mdelete-licence.sh^[[0m   ^[[01;34msrc^[[0m^M
^[[00;32mbuild-doc.sh^[[0m     ^[[00;32mclean^[[0m             ^[[01;34mdoc-src^[[0m             ^[[00mtest.ini^[[0m^M
spl62 lepl: exit^M

Script done on Thu 09 Jun 2011 09:47:29 AM CLT
spl62 tmp: col -b 
                                
andrew cooke (1121 rep)
Jun 9, 2011, 01:51 PM • Last activity: Jul 25, 2025, 08:26 AM
0 votes
1 answers
72 views
Need Help Understanding script that reads output of cursor position ANSI escape code
I have this code, which does what I want, but I don't entirely understand it: ``` #!/usr/bin/env bash echo -ne "\033[6n" # Ask the terminal to print out the cursor's position # The response looks like ^[[n;mR - where n = row, m = col read -s -d\[ garbage # Read the response silently, discarding the...
I have this code, which does what I want, but I don't entirely understand it:
#!/usr/bin/env bash

echo -ne "\033[6n"            # Ask the terminal to print out the cursor's position
                              # The response looks like ^[[n;mR - where n = row, m = col
read -s -d\[ garbage          # Read the response silently, discarding the first part of the response, leaving n;mR
read -s -d ';' row            # Read some more, silently, until we get to the character ';', storing what we read in the variable "row"
read -s -d R col              # Read some more, silently, until we get to the character 'R', storing what we read in the variable "col"
echo "The (row,col) coords are ($row,$col)"
If I run echo -ne "\033[6n" at a shell prompt, I get kind of what the comments say I'll get, but not exactly:
$ echo -ne "\033[6n"
^[[11;1R$ ;1R
If I put that in a script (I'll call it "splurt.sh"), and run the script, the results are the same:
#!/usr/bin/env bash

echo -ne "\033[6n"

echo
$ ./splurt.sh 

^[[16;1R$ ;1R
So at this point, I just accept that the line works, even if it's not displaying as I would expect it to. But then comes the next line:
read -s -d\[ garbage          # Read the response silently, discarding the first part of the response, leaving n;mR
I kind of understand that "read" reads input from the console (keyboard, usually, I would think), but somehow it seems to be reading from the console's monitor? I understand the "-s" means to read the console silently, but I don't quite comprehend that. I put this into my short script:
#!/usr/bin/env bash

echo -ne "\033[6n"
read -s -d\[ garbage
echo
and I get:
$ ./splurt.sh 

$ 25;1R
If I leave out the "-s", I get:
$ ./splurt.sh 
^[[27;1R
$ 27;1R
So I can see that the "-s" does prevent the echo of "^[[27;1R" to the screen, but I don't grok that, because I thought the echo -n e "\033[6n" had already printed that out to the screen (although apparently not, because I don't see it until the "read" reads it?). I do understand (finally! I understand something!) that the "-d" only reads up to the specified delimiter (usually a newline, as I understand it, unless specified by the "-d"), and that what gets read is placed into the variable "garbage" (which is henceforth ignored), but I don't understand how "\[" causes the read to go all the way through "^[[", instead of stopping at "^[", to leave "[27:1R" instead of what it actually leaves of "27:1R". And then the rest of the script I have a fairly good grip on, methinks. Anyone want to take a stab at helping me to understand better what's going on? Thanks!
user153064 (13 rep)
Jul 8, 2025, 01:28 PM • Last activity: Jul 8, 2025, 08:05 PM
70 votes
5 answers
47779 views
Where do I find a list of terminal key codes to remap shortcuts in bash?
For example: "\e[1;5C" "\e[Z" "\e-1\C-i" I only know bits and pieces, like `\e` stands for escape and `C-` for Ctrl , but what are these numbers (`1`) and letters (`Z`)? What are the `;`, `[` and `-` signs for? Is there only trial and error, or is there a complete list of bash key codes and an expla...
For example:
"\e[1;5C"
"\e[Z"
"\e-1\C-i"
I only know bits and pieces, like \e stands for escape and C- for Ctrl, but what are these numbers (1) and letters (Z)? What are the ;, [ and - signs for? Is there only trial and error, or is there a complete list of bash key codes and an explanation of their syntax?
bug (2698 rep)
May 21, 2013, 10:25 AM • Last activity: May 14, 2025, 06:58 PM
1 votes
1 answers
117 views
Multiline command substitution - syntax errors with mysterious `+1`
I'm trying to break a long command substitution on to multiple lines, as discussed in [this answer](https://unix.stackexchange.com/a/82183/128023). In a plain command pipeline, both explicit (`\`) and implicit line continuation work fine: ```shell $ echo 'blah foo bar' \ > | grep -F 'blah' blah foo...
I'm trying to break a long command substitution on to multiple lines, as discussed in [this answer](https://unix.stackexchange.com/a/82183/128023) . In a plain command pipeline, both explicit (\) and implicit line continuation work fine:
$ echo 'blah foo bar' \
> | grep -F 'blah'
blah foo bar

$ echo 'blah foo bar' |
> grep -F 'blah'
blah foo bar
Inside of a command substitution, they both fail, and slightly differently:
$ foo=$(echo 'blah foo bar' \
> | grep -F 'blah')
-bash: 4131
| grep -F 'blah') +1 : syntax error: invalid arithmetic operator (error token is "'blah') +1 ")

$ foo=$(echo 'blah foo bar' |
> grep -F 'blah')
-bash: 4133
grep -F 'blah') +1 : syntax error in expression (error token is "grep -F 'blah') +1 ")
I'm on bash 5.2.21 and .37. I'm testing at the command prompt, but my real use case is this in .bashrc:
SSH_AUTH_SOCK="$(\ls -l /tmp/ssh-*/agent.* 2> /dev/null | grep -F "$USER" | head -1 | awk '{print $NF}')"
What's happening? Where did the +1 come from? And is there a way to do this correctly? (I'm interested in portable solutions or bash-specific.) ### Edit: More context I'm using [Oh My Posh] and [Atuin] with [bash-preexec], all of which do something at prompt time. My example actually looks like this: Screenshot of example showing fancy prompt They're set up in .bashrc like so:
eval "$(oh-my-posh init bash --config "${omp_config}")"                                                                                                                                                          # Get bash history number:
set_poshcontext () { export _myhistcmd=$(( $(fc -l -1 | cut -f1) +1 )); }
#                                                 Note the +1! ^^

[ -r "${HOMEBREW_PREFIX}/etc/profile.d/bash-preexec.sh" ] && . "${HOMEBREW_PREFIX}/etc/profile.d/bash-preexec.sh"
eval "$(atuin init bash --disable-up-arrow)"
To @Stéphane Chazelas questions:
$ typeset -p PS{1..4}
declare -- PS1="\$(_omp_get_primary)"
declare -- PS2="\$(_omp_get_secondary)"
-bash: typeset: PS3: not found
declare -- PS4="+ "

$ echo "$PROMPT_COMMAND"
__bp_precmd_invoke_cmd
_omp_hook
__bp_interactive_mode

$ trap
trap -- '__bp_preexec_invoke_exec "$_"' DEBUG
Jacktose (533 rep)
Apr 30, 2025, 11:58 PM • Last activity: May 2, 2025, 05:58 PM
9 votes
3 answers
827 views
How to delete buffered text written to terminal during script execution
Let's say hypothetically I'm executing a long-running bash script, and while it's running, I accidentally right click on the terminal and paste a sensitive password (freshly copied out of a password manager) into the terminal. ``` ~$ process_data.sh [useful data 1] [useful data 2] ... [useful data N...
Let's say hypothetically I'm executing a long-running bash script, and while it's running, I accidentally right click on the terminal and paste a sensitive password (freshly copied out of a password manager) into the terminal.
~$ process_data.sh
[useful data 1]
[useful data 2]
...
[useful data N-1]
hunter2[useful data N]
[useful data N+1]
**Is there any way to remove the text (hunter2) from showing up on the terminal, while preserving the useful data?** Ctrl-L and clear delete everything on the screen. Ctrl-H works to delete characters in the current line, but I can't move the cursor up. I don't need to delete the text from ~/.bash_history, I just want the useful data to show up in the terminal without the offending text. I assume the answer may depend on the capabilities/implementation of the terminal. I'm on Ubuntu 18, but solutions for any common terminal would be appreciated.
Ivoirians (193 rep)
Apr 23, 2025, 08:29 PM • Last activity: Apr 26, 2025, 06:39 AM
5 votes
3 answers
5944 views
Why I can't send escape sequences from keyboard, but can do it from another tty?
I am trying to understand how terminal works by doing different tricks, like writing from one tty to another, changing settings `tty1` from `tty2`, etc. Also, I am trying to change a color by sending escape sequence from keyboard, directly. That is, not by `echo -e '\e[0;31m'` command, but by direct...
I am trying to understand how terminal works by doing different tricks, like writing from one tty to another, changing settings tty1 from tty2, etc. Also, I am trying to change a color by sending escape sequence from keyboard, directly. That is, not by echo -e '\e[0;31m' command, but by direct keyboard input. It doesn’t work. **I do this steps:** 1. Open tty1 and tty2 2. In the tty2 put bash into the sleep mode, by sleep 10m. Type word 'one'. enter image description here 3. Go to tty1, do echo -n ^[[0;31m > /dev/tty2. The first character ^[ is typed by this way Ctrl + v Esc 4. Return to tty2, type word 'two'. Yes - the color has been changed to red by command from another tty. enter image description here 5. Repeat steps 3,4, but with green color and word 'three' enter image description here 6. And finally, I am trying to send the escape sequence not by another tty, but from keyboard directly - by typing ^[[0;37m in tty2. I do everything the same way - Esc (Ctrl + v doesn't needed, because readline is sleeping), then [0;37m, but get this: enter image description here **Question:** Why does it work this way? All characters the same, terminal state the same, but in one case terminal get escape sequence, and in another case don't. ------------------------------------ **Edit** The question was answered here: [Echoed escape sequences doesn't interpreted in Linux tty](https://unix.stackexchange.com/q/518418/109397) .
MiniMax (4225 rep)
Jun 7, 2017, 09:07 PM • Last activity: Mar 10, 2025, 03:06 PM
2 votes
0 answers
266 views
How to convert an escape sequence to a human-readable keystroke?
I am looking for a Linux command, or a zsh command, or at least an online service that given: - an escape sequence like "^[^[[1~", "\M-C\M-^X" or "^[[1;5F" (these are not made up, but output from my `bindkey`) - and my setup (PC keyboard, Konsole terminal and any other info that can have effect on g...
I am looking for a Linux command, or a zsh command, or at least an online service that given: - an escape sequence like "^^[[1~", "\M-C\M-^X" or "^[[1;5F" (these are not made up, but output from my bindkey) - and my setup (PC keyboard, Konsole terminal and any other info that can have effect on generation of escape sequences) can show me a corresponding keystroke or their combination. Bonus points if it can explain colors etc, but my main interest is keyboard shortcuts. Context: I am learning zsh key bindings that are heavily customized in [zsh4humans (thus most of online zsh cheatsheets do not apply). I know that bindkey can show me a list of all shortcuts, and info zsh provides a widget's meaning. I know that I can convert a keystroke to an escape sequence myself using showkey -a (but ofc it uses the caret notation that may not match a notation shown by zsh). So, basically I'm looking for a command that performs an opposite function of showkey. Thanks in advance.
Radagast (41 rep)
Feb 18, 2024, 01:23 PM • Last activity: Mar 1, 2025, 07:49 AM
21 votes
4 answers
19997 views
The easiest way to clear scrollback buffer of terminal + some deeper explanation?
Why bother? Clearing scrollback buffer is handy in many ways, for example, when I wish to run some command with long output, and want to quickly scroll to start of this output. When scrollback buffer is cleared, I can just scroll to top, and will be done. Some considerations: There is `clear` comman...
Why bother? Clearing scrollback buffer is handy in many ways, for example, when I wish to run some command with long output, and want to quickly scroll to start of this output. When scrollback buffer is cleared, I can just scroll to top, and will be done. Some considerations: There is clear command, according to man, > **clear** clears your screen if this is possible, including its scrollback buffer (if the extended "E3" capability is defined). In gnome-terminal clear does *not* clear scrollback buffer. (What is "E3" capability, though?) There is also reset, which clears, but it does a little bit more than that, and it is really slow (on my system it takes more than a second, which is significant delay for humans to be noticed). And there is echo -ne '\ec' or echo -ne '\033c', which does the job. And indeed it is much faster than reset. The question is, what is \ec sequence, how it differs from what clear and reset does, and why there is no separate command for it? There is also readline's C-l key sequence, which by default bound to clear-screen command (I mean, readline command, not shell command). What is this command? Which escape sequence it emits? How does it actually work? Does it run shell command? Or what? Again, in gnome-terminal, it seems like it works just by spiting out blank lines until prompt appear in top line of terminal. Not sure about other terminal emulators. This is very cumbersome behavior. It pollutes scrollback with chunks of emptiness, so you must scroll up more, and more. It is like a hack, rather than clean solution. Another question is, is there a readline command for mentioned \ec sequence? I want to bound it to C-l instead because I _always_ want to clear scrollback buffer when I clear the screen. And another question is how to _just type_ such escape sequence into terminal, to perform desired action? Then do not have to think about binding C-l to another readline command (if such command exists). I tried typing Esc, then c but this does not work. **UPDATE** This question answered mostly here: https://unix.stackexchange.com/a/375784/257159 . It is very good answer which explains almost all questions asked here.
Anthony (687 rep)
Dec 13, 2017, 05:52 PM • Last activity: Feb 28, 2025, 06:38 PM
1 votes
1 answers
71 views
Zsh’s autocompletion options from variable
## General overview I have to set a values auto-completion in zsh for a command (in the following minimal example, I will show it with `testcmd`). So my current code work quiet well with hardcoded values: #### Current auto-completion code (the one who works) ```zsh function testcmd() { echo "Nothing...
## General overview I have to set a values auto-completion in zsh for a command (in the following minimal example, I will show it with testcmd). So my current code work quiet well with hardcoded values: #### Current auto-completion code (the one who works)
function testcmd()
{
	echo "Nothing to do, just a test command"
}


_test_complete() {
	_values \
		"Possible values" \
		foo'[Foo]' \
		bar'[Bar baz]' \
}

compdef _test_complete testcmd
#### Current auto-completion behavior When I type testcmd , I correctly get the following desired rendering:
$ testcmd
Possible values
bar  -- Bar baz
foo  -- Foo
## What is the goal I search to reach But as you see, the values are hard-coded inside the function, ideally, the function should retrieve them from a variable. ## What I already try So naturaly, I put the values inside the variable values_variable as following: #### The tried code (the one who doesn’t works)
function testcmd()
{
	echo "Nothing to do, just a test command"
}


_test_complete() {
	local values_variable
	values_variable="foo'[Foo]' \
		bar'[Bar baz]' \ "
	_values \
		"Possible values" \
		${values_variable}
}

compdef _test_complete testcmd
#### The behavior of the tried code But then, when I try testcmd it completely fail:
$ testcmd
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
$ testcmd
### What I _also_ did - I tried to escape the spaces with echo ${values_variable} | sed "s/ /\\ /g" ; - I tried to use $values_variable with the eval command to pretend as if it content was directly typed inside the _values definition ; - I tried both eval and escaping with eval $(echo ${values_variable} | sed "s/ /\\ /g") ; - I tried to eveal line by line with a loop :
echo "$values_variable" | while read -r line; do
    eval "$line"
  done
- Expansion with ${(@f)values_variable}̀ ; - Many other ideas. But it also failed. ### The nearest solution I found In [_How to pass the contents of a file using cat to _values (zsh completion)_](https://stackoverflow.com/questions/21356370/how-to-pass-the-contents-of-a-file-using-cat-to-values-zsh-completion) tread I found a solution for values imported from an external file, but the user seems to be fronted to the same space escape problem. However, I can’t adjust it to the case with the $values_variables internal variable. I naturally tried this one, who doesn’t works either:
_test_complete() {
	local values_variable
	values_variable="foo'[Foo]' \
		bar'[Bar baz]' \ "
	OLD_IFS=$IFS
	IFS=$'\n'
	_values \
		"Possible values" \
		${values_variable}
	IFS=$OLD_IFS
}
## The question How can I load the values to give to _values inside auto-completion function from a variable?
fauve (1529 rep)
Feb 2, 2025, 05:54 PM • Last activity: Feb 3, 2025, 07:10 PM
20 votes
3 answers
14505 views
How to determine the current color of the console output?
I know that, if a coloured terminal is available, one can [colour the output of it using escape characters][1]. But is there a possibility to find out, which colour the output is currently being displayed as? Or better, what colour the text would be, if I would output it right now? I'm asking to not...
I know that, if a coloured terminal is available, one can colour the output of it using escape characters . But is there a possibility to find out, which colour the output is currently being displayed as? Or better, what colour the text would be, if I would output it right now? I'm asking to not break any previous colour settings, when using these escape characters. The 'default foreground colour' escape character is getting it's information from the colour scheme, rather than the text colour before I changed it.
Minix (6065 rep)
Dec 10, 2014, 02:04 PM • Last activity: Feb 3, 2025, 05:13 AM
6 votes
1 answers
5358 views
Escape Code to Change Cursor Shape
I know that Konsole supports an escape code like `"\ ]50;CursorShape=0\x7"` that will change the cursor shape. I was wondering if any other terminal emulators support changing the cursor shape via an escape code?
I know that Konsole supports an escape code like "\]50;CursorShape=0\x7" that will change the cursor shape. I was wondering if any other terminal emulators support changing the cursor shape via an escape code?
Zameer Manji (777 rep)
Sep 29, 2012, 06:03 PM • Last activity: Jan 17, 2025, 08:17 PM
8 votes
2 answers
1193 views
Why does the special character `?` need to be escaped in grep, but not `.` or `*`?
The only file in my current working directory is called `test.txt` and its content is simply: ```none This is a little test file. ``` * Executing ```lang-sh grep -in * -e 'te.?t file' ``` gives **no match**. * But escaping `?` works: ```lang-sh grep -in * -e 'te.\?t file' ``` **gives match** **Crazy...
The only file in my current working directory is called test.txt and its content is simply:
This is a little test file.
* Executing
-sh
    grep -in * -e 'te.?t file'
gives **no match**. * But escaping ? works:
-sh
    grep -in * -e 'te.\?t file'
**gives match** **Crazy:** With the star *, things are the other way round! * Without escaping:
-sh
    grep -in * -e 'te.*t file'
**gives match** * and escaping * does not work:
-sh
    grep -in * -e 'te.\*t file'
**no match** Why are the meta-characters ? and * treated differently with respect to escaping?
Dominic van der Zypen (195 rep)
Nov 21, 2024, 01:51 PM • Last activity: Nov 28, 2024, 01:56 PM
10 votes
3 answers
2784 views
Why do `ctrl+[` and `ESC` both produce `^[`?
In bash, I press ctrl + v to start verbatim insert. In the verbatim mode, I press the Esc key and bash shows `^[`. I redirect it to file `esc`. Also in the verbatim mode, I press ctrl key with [ key, and bash shows `^[`. I redirect it to file `ctrl`. Next, I compare the two files, and they are the s...
In bash, I press ctrl+v to start verbatim insert. In the verbatim mode, I press the Esc key and bash shows ^. I redirect it to file esc. Also in the verbatim mode, I press ctrl key with [ key, and bash shows ^[. I redirect it to file ctrl. Next, I compare the two files, and they are the same! $ echo '^[' > esc $ echo '^[' > ctrl $ diff esc ctrl $ Why do Ctrl+[ and Esc produce the same content? Is ^[ here [the C0 and C1 control codes ? If so, the wiki article says ^[ is Escape, so why is ctrl+[ also Escape? The root problem is that I want to check and create a key binding. (zsh)$ bindkey -L ... bindkey "^['" quote-line ... So do I need to type ESC+' or ctrl+[+'?
Gqqnbig (249 rep)
Oct 16, 2022, 11:42 AM • Last activity: Nov 14, 2024, 12:12 PM
0 votes
3 answers
148 views
How to escape both single quotes and exclamation marks in bash
I have a long command and I just want to use `alias` to shorten it. But the command contains single quotes and exclamation marks. The origin command is `ldapsearch -x -H ... -w 'abc!123'`. I tried `alias search='ldapsearch -x -H ... -w \'abc!123\''` or `alias search="ldapsearch -x -H ... -w 'abc!123...
I have a long command and I just want to use alias to shorten it. But the command contains single quotes and exclamation marks. The origin command is ldapsearch -x -H ... -w 'abc!123'. I tried alias search='ldapsearch -x -H ... -w \'abc!123\'' or alias search="ldapsearch -x -H ... -w 'abc!123'" and so on. But none of them works for me.
Donghua Liu (103 rep)
May 31, 2024, 06:27 AM • Last activity: Nov 9, 2024, 01:34 PM
0 votes
0 answers
26 views
When using backspace you can see prompt flicker in custom REPL
I'm not sure if this is the right place to ask this. Or should I ask on StackOverflow and show my code. But maybe someone will be able to tell my by just looking at the symptoms. I have a custom REPL in Node.js where I do a lot of stuff to add syntax-highlighting and auto indentation. And recently n...
I'm not sure if this is the right place to ask this. Or should I ask on StackOverflow and show my code. But maybe someone will be able to tell my by just looking at the symptoms. I have a custom REPL in Node.js where I do a lot of stuff to add syntax-highlighting and auto indentation. And recently noticed that when I press backspace, the cursor jump to the beginning of the line where prompt is located and the whole like flicker. This only happens when doing backspace. And it started to happen probably a few months ago. enter image description here On the gif it looks like the whole prompt disappear, it's very fast not like this, but noticeable. It's not because of a new version of Node, because it works the same on older versions. So something needed to happen to my GNU/Linux system, something got updated and introduced this behavior. I've tested this on Fedora Xfce terminal, on xterm it's not always as bad, the cursor doesn't jump on every backspace, but sometimes it does. Any tips what I should look at and maybe where to report this if it's a bug, would be great. Also, maybe someone can explain what is happening and if this is not a bug. **EDIT** I've noticed that the same happen with Fedora 41 dnf install when it display multiple progress bars, everything flickers during animation. I want to report this a bug, but I need help, where I should report this? Should I write issue with Fedora or find the upstream project?
jcubic (10310 rep)
Sep 27, 2024, 11:01 AM • Last activity: Oct 30, 2024, 07:15 PM
3 votes
2 answers
1774 views
How to generate double quoted JSON string with escaped double quotes?
I sometimes need to write JSON strings containing a string value that is itself a JSON string. e.g. In the following JSON object: echo '{"rec" : " {\"id\": 1, \"name\": \"x\"}" }' | jq . { "rec": " {\"id\": 1, \"name\": \"x\"}" } the value at `"rec"` is a json in string form ` {\"id\": 1, \"name\":...
I sometimes need to write JSON strings containing a string value that is itself a JSON string. e.g. In the following JSON object: echo '{"rec" : " {\"id\": 1, \"name\": \"x\"}" }' | jq . { "rec": " {\"id\": 1, \"name\": \"x\"}" } the value at "rec" is a json in string form {\"id\": 1, \"name\": \"x\"}". It is tedious to write such escaped json strings, as one needs to escape each double quote " to \". My question is: Given the content { "id": 1, "name": "x"}, is there an automatic way using bash/jq to properly escape the quotes and get the quoted form usable as a JSON string value "{\"id\": 1, \"name\": \"x\"}"?
tinlyx (1072 rep)
Oct 7, 2024, 08:13 AM • Last activity: Oct 7, 2024, 09:58 AM
0 votes
1 answers
90 views
Does the RESTORE CURSOR escape sequence also restore formatting?
When using the save/restore cursor escape sequences `\e7` & `\e8`, I find that it not only restores the cursor position, but it also restores the text formatting (colors). I've tried under WSL, vscode in Windows, and in an online Ubuntu VM. Is this standardized (for some value of 'standard')? Can it...
When using the save/restore cursor escape sequences \e7 & \e8, I find that it not only restores the cursor position, but it also restores the text formatting (colors). I've tried under WSL, vscode in Windows, and in an online Ubuntu VM. Is this standardized (for some value of 'standard')? Can it be relied on in most terminals in use today?
Reinstate Monica (733 rep)
Oct 6, 2024, 06:45 PM • Last activity: Oct 6, 2024, 07:49 PM
-1 votes
2 answers
138 views
How to interpret escape characters in a string and display them as newlines, color etc
i `grep` something like: ```bash str='"a\nb"' echo ${str} | grep -E -o '".*"' ``` and got: ```bash "a\nb" ``` i wish the result displays like: ```bash a b ``` how could this be done? thanks p.s. actually, the `str` is ```bash str='"\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\...
i grep something like:
str='"a\nb"'
echo ${str} | grep -E -o '".*"'
and got:
"a\nb"
i wish the result displays like:
a
b
how could this be done? thanks p.s. actually, the str is
str='"\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\r\33[1A\33[K\33[32m[58,378 / 59,145]\33[0m 12 actions running\n    Compiling interface/detail/type_support.c; 5s linux-sandbox\n    Compiling interface/detail/type_c.cpp; 5s linux-sandbox\n    ; 2s linux-sandbox ...\n"'
i think the \33[...part is related to color on shell, which is more complicated...could the \n and \r as well as color display as human-readable by grep or jq?
furynerd (21 rep)
Aug 23, 2024, 09:21 AM • Last activity: Aug 23, 2024, 02:14 PM
0 votes
0 answers
35 views
Bash misinterpreting lenght of PS1 when using ANSI escape sequences
if i use the following ``` export PS1='\033[36m$ \033[0m' ``` Bash misunderstands the length of the prompt and starts line-wrapping too early. My assumption was that it is counting every character, but comparing `${#PS1}` to `$COLUMNS` shows that not to be the case. if i instead use the following ``...
if i use the following
export PS1='\033[36m$ \033[0m'
Bash misunderstands the length of the prompt and starts line-wrapping too early. My assumption was that it is counting every character, but comparing ${#PS1} to $COLUMNS shows that not to be the case. if i instead use the following
export PS1='$ '
there is no issue additionally, if i use dash shell, this does not happen, so i know it is an issue with Bash, and not my terminal etc. however, if i launch dash in vi mode with dash -V or set -o vi, the escape characters are ignored and not sent to my terminal at all. I assume this is a bug in dash though, so i reported this on the dash mailing list. thanks
user21749640 (101 rep)
Aug 20, 2024, 09:17 PM
0 votes
0 answers
132 views
Node.js recently started emitting all stderr in red text. How to handle this cleanly?
I use ANSI colorized text output in logs and other output that I emit on stderr in some of my programs. Since I've been doing this kind of thing for years, I am pretty familiar with the capabilities. The trouble with the change in behavior that node.js introduced is that now all stderr output is get...
I use ANSI colorized text output in logs and other output that I emit on stderr in some of my programs. Since I've been doing this kind of thing for years, I am pretty familiar with the capabilities. The trouble with the change in behavior that node.js introduced is that now all stderr output is getting wrapped in red foreground (text) color codes, and it's generally not practical or possible to refactor entire applications to become aware of whether this thing is going to happen to the stderr stream or not. If I am emitting some output with some colored text, e.g. Error:\e[33m message\e[39m at position That writes "Error: message at position" where "message" is in yellow. When node.js causes this whole thing to be then wrapped in \e[31m \e[m (or \e39m), what I will end up with on the terminal is "Error:" in red, "message" in yellow, and "at position" in the normal text color. This is unexpected and looks wrong, but basically it's impossible to fix unless I change the behavior of my code to know it is emitting this on stderr and if it knows that the version of node running the code is new enough it should change to red text color instead of resetting the text color. One way to express this issue is the fact that ANSI escapes are a stream protocol instead of being a tree/stack like HTML or something, whereby the yellow color could be popped leaving the base red color there. One approach I could take is to change to not use text colors in stderr output, as the ansi states for background color, boldedness, italicness, etc. are not going to mess with the red text color when they get reset with their appropriate reset codes. This is still not ideal though, because I definitely have situations where I want the colorized util.inspect strings in certain error outputs...
Steven Lu (2422 rep)
Jun 19, 2024, 04:47 PM
Showing page 1 of 20 total questions