How do I break up an extremely long string literal in bash?
You can use a variable :
file=extremely/long/file/name
file+=/that/i/would/like/to/be/able/to/break
file+=/up/if/possible
mycommand\
--server myserver\
--filename $file\
--flag flag
Bash: split long string argument to multiple lines?
You can assign your string to a variable like this:
long_arg="my very long string\
which does not fit\
on the screen"
Then just use the variable:
mycommand "$long_arg"
Within double quotes, a newline preceded by a backslash is removed. Note that all the other white space in the string is significant, i.e. it will be present in the variable.
How to split string across multiple lines in bash
Using inline document with the <<-
operator:
while getopts 'd:h' argv; do
case "${argv}" in
d) local target_dir="${OPTARG}" ;;
h)
cat <<-EOT
Usage: remove_file_end_strings [ -d <work directory> ] <string to remove>
EOT
esac
done
See man bash
and look for Here Documents
:
If the redirection operator is <<-, then all leading tab characters
are stripped from input lines and the line containing delimiter. This
allows here-documents within shell scripts to be indented in
a natural fashion.
If a break in the line is required, pipe a sed
command that will remove tabs in between the string:
while getopts 'd:h' argv; do
case "${argv}" in
d) local target_dir="${OPTARG}" ;;
h)
cat <<-EOT | sed 's/\t*\([^\t]*\)/ \1/2g'
Usage: remove_file_end_strings [ -d <work \
directory> ] <string to remove>
EOT
esac
done
Multi-line string with extra space (preserved indentation)
Heredoc sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program like ex or cat
cat << EndOfMessage
This is line 1.
This is line 2.
Line 3.
EndOfMessage
The string after <<
indicates where to stop.
To send these lines to a file, use:
cat > $FILE <<- EOM
Line 1.
Line 2.
EOM
You could also store these lines to a variable:
read -r -d '' VAR << EOM
This is line 1.
This is line 2.
Line 3.
EOM
This stores the lines to the variable named VAR
.
When printing, remember the quotes around the variable otherwise you won't see the newline characters.
echo "$VAR"
Even better, you can use indentation to make it stand out more in your code. This time just add a -
after <<
to stop the tabs from appearing.
read -r -d '' VAR <<- EOM
This is line 1.
This is line 2.
Line 3.
EOM
But then you must use tabs, not spaces, for indentation in your code.
Bash: split a (multiline) string into array with a multichar delimiter
Assuming that the source string is in the variable s
, the following bash script will populate the array variable a
as required:
a=()
i=0
while read -r line
do
a[i]="${a[i]}${line}"$'\n'
if [ "$line" == "(StopValues)" ]
then
let ++i
fi
done <<< "$s"
How do I break a string in YAML over multiple lines?
Using yaml folded style. The indention in each line will be ignored. A line break will be inserted at the end.
Key: >
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with only a single carriage return appended to the end.
http://symfony.com/doc/current/components/yaml/yaml_format.html
You can use the "block chomping indicator" to eliminate the trailing line break, as follows:
Key: >-
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with NO carriage returns.
In either case, each line break is replaced by a space.
There are other control tools available as well (for controlling indentation for example).
See https://yaml-multiline.info/
How to split a string into an array in Bash?
IFS=', ' read -r -a array <<< "$string"
Note that the characters in $IFS
are treated individually as separators so that in this case fields may be separated by either a comma or a space rather than the sequence of the two characters. Interestingly though, empty fields aren't created when comma-space appears in the input because the space is treated specially.
To access an individual element:
echo "${array[0]}"
To iterate over the elements:
for element in "${array[@]}"
do
echo "$element"
done
To get both the index and the value:
for index in "${!array[@]}"
do
echo "$index ${array[index]}"
done
The last example is useful because Bash arrays are sparse. In other words, you can delete an element or add an element and then the indices are not contiguous.
unset "array[1]"
array[42]=Earth
To get the number of elements in an array:
echo "${#array[@]}"
As mentioned above, arrays can be sparse so you shouldn't use the length to get the last element. Here's how you can in Bash 4.2 and later:
echo "${array[-1]}"
in any version of Bash (from somewhere after 2.05b):
echo "${array[@]: -1:1}"
Larger negative offsets select farther from the end of the array. Note the space before the minus sign in the older form. It is required.
Related Topics
Mechanism of Clipboard of Xwindow
What Is the Pid in the Host, of a Process Running Inside a Docker Container
Ssh Error When Executing a Remote Command: "Stdin: Is Not a Tty"
Isolate Kernel Module to a Specific Core Using Cpuset
Will Adding the -Rdynamic Linker Option to Gcc/G++ Impact Performance
Command to Get Time in Milliseconds
How to Use Sed to Remove the Last N Lines of a File
Subtract Days from a Date in Bash
Opening a .Tar.Gz File with a Single Command
How to Find the Processor/Chip Architecture on Linux
How to Automate the Installation of Eclipse Plugins with Command Line
How to Export Database Schema in Oracle to a Dump File
How to Divide in the Linux Console
Genymotion Throws Libssl_Conf.So: Cannot Open Shared Object File: No Such File or Directory
Can't Remove a Directory in Unix
How to Send a Sequence of at Commands to a Serial Port in Bash