How to pass an argument in Bash CLI that contains an LF character? Something like: myprog foo\nbar
I tried this:
myprog `printf 'foo\nbar'`
myprog foo\nbar
I used this bash program to test the results:
#myprog
echo $*
and node.js program as well
#!/usr/bin/env node
console.log(process.argv[2])
It does not work.
$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.
The \ at the end of a line escapes the actual newline character that you type in using the enter key.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
In bash
use the ANSI C like strings, with the $'...'
notation as below. This is especially useful when you want to pass special characters as arguments to some programs.
myProgram $'foo\nbar'
You can see the hexdump
of the string formed. Don't confuse the trailing new line, since it is introduced by the here-string <<<
construct in bash
$ hexdump -c <<< $'foo\nbar'
0000000 f o o \n b a r \n
0000008
The following escape sequences are also supported, updating the list here, since it is not available in the duplicated one.
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| code | meaning |
| | |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| \" | double-quote |
| \' | single-quote |
| \\ | backslash |
| \a | terminal alert character (bell) |
| \b | backspace |
| \e | escape (ASCII 033) |
| \E | escape (ASCII 033) \E is non-standard |
| \f | form feed |
| \n | newline |
| \r | carriage return |
| \t | horizontal tab |
| \v | vertical tab |
| \cx | a control-x character, for example, $'\cZ' to print the control sequence composed of Ctrl-Z (^Z) |
| \uXXXX | Interprets XXXX as a hexadecimal number and prints the corresponding character from the character set (4 digits) (Bash 4.2-alpha)|
| \UXXXXXXXX | Interprets XXXX as a hexadecimal number and prints the corresponding character from the character set (8 digits) (Bash 4.2-alpha)|
| \nnn | the eight-bit character whose value is the octal value nnn (one to three digits) |
| \xHH | the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With