When creating Bash scripts, knowing how to deal with strings can be a boon. Fortunately, the Bash shell offers myriad options. The technique you choose is often determined by the length of the string and your personal preference.
The following strategies represent some of the options available and assume the string should be saved to a variable. All strings cited are from Frank Rieger's Welcome to the world of tomorrow (2005).
Note: If you are not familiar with the GNU/Linux command line interface, or you intend to use a script obtained from this site, review the Conventions page before proceeding.
Long Strings
For a long string, a here document is an option:
theme='threat'
read -d '' ls1 << EOF
"Terrorism" is the theme of the day, others will follow. And these 'themes' can and will be used to mold the western societies into something that has never been seen before: a democratically legitimated police state, ruled by an unaccountable elite with total surveillance, made efficient and largely unobtrusive by modern technology.
Cooking up the "terrorist ${theme}" by apparently stupid foreign policy and senseless intelligence operations provides a convenient method to get through with the establishment of a democratically legitimized police state. No one cares that car accidents alone kill many more people than terrorists do. The fear of terrorism accelerates the changes in society and provides the means to get the suppression tools required for the coming waves of trouble.
What we call today 'anti-terrorism measures' is the long-term planned and conscious preparation of those in power for the kind of world described above.
EOF
The setup for a here document has a few parts:
<<denotes our here document, for which we useEOFas the delimiter to mark the beginning and end of our input stream literal.- Our here document is sent to the
readcommand. We set an empty string as the delimiter (-d '') to ensure that multiple lines are read, and save the read string into thels1variable.
An alterative solution is to use weak quotes. This technique has the benefit of being comparable to what is used in non-shell programming languages:
no_place_to_hide='nothing to hide'
ls2="\
The key question for establishing an effective surveillance based police state is to keep it low-profile enough that \"the ordinary citizen\" feels rather protected than threatened, at least until all the pieces are in place to make it permanent. First principle of 21st century police state: All those who \"have ${no_place_to_hide}\" should not be bothered unnecessarily.
This goal becomes even more complicated as with the increased availability of information on even minor everyday infringements, the \"moral\" pressure to prosecute will rise. Intelligence agencies have always understood that effective work with interception results requires a thorough selection between cases where it is necessary to do something, and those (the majority) where it is best to just be silent and enjoy.\
"
Unlike with the here document method, you will need to escape any double quotes in your string, as well as any unwanted newlines.
Both the here document and weak quotes approaches support variable and command substitution.
Short Strings
String appending using the += operator works well for shorter strings and is often used in non-shell programming languages:
ss1='Only when the oppression becomes too burdensome and open, there might '
ss1+='be a chance to get back to the overall progress of mankind earlier. '
ss1+='If the powers that be are able to manage the system smoothly and '
ss1+='skillfully, we cannot make any prediction as to when the new dark ages '
ss1+='will be over...But for the rest of us, the only realistic option '
ss1+='is to try to live in, with, and from the world as bad as it has become. '
ss1+='We need to build our own communities nonetheless, virtual or real ones.'
Alternatively, command substitution with a code block and multiple echo commands can work, too:
ss2="$(
{
echo -n 'So one thing that we can do to help societies progress along '
echo -n 'is to provide tools, knowledge and training for secure '
echo -n 'communications to every political and social movement that '
echo -n 'shares at least some of our ideals.'
}
)"
Putting It All Together
We can run the following code to display the strings in the variables we created:
fmt -w 79 <<< "${ls1}"
echo -e '\n---\n'
fmt -w 79 <<< "${ls2}"
echo -e '\n---\n'
fmt -w 79 <<< "${ss1}"
echo -e '\n---\n'
fmt -w 79 <<< "${ss2}"
The output would look like this:
"Terrorism" is the theme of the day, others will follow. And these 'themes'
can and will be used to mold the western societies into something that
has never been seen before: a democratically legitimated police state,
ruled by an unaccountable elite with total surveillance, made efficient
and largely unobtrusive by modern technology.
Cooking up the "terrorist threat" by apparently stupid foreign policy and
senseless intelligence operations provides a convenient method to get through
with the establishment of a democratically legitimized police state. No one
cares that car accidents alone kill many more people than terrorists do. The
fear of terrorism accelerates the changes in society and provides the means
to get the suppression tools required for the coming waves of trouble.
What we call today 'anti-terrorism measures' is the long-term planned and
conscious preparation of those in power for the kind of world described above.
---
The key question for establishing an effective surveillance based police
state is to keep it low-profile enough that "the ordinary citizen" feels
rather protected than threatened, at least until all the pieces are in
place to make it permanent. First principle of 21st century police state:
All those who "have nothing to hide" should not be bothered unnecessarily.
This goal becomes even more complicated as with the increased availability
of information on even minor everyday infringements, the "moral" pressure
to prosecute will rise. Intelligence agencies have always understood that
effective work with interception results requires a thorough selection
between cases where it is necessary to do something, and those (the majority)
where it is best to just be silent and enjoy.
---
Only when the oppression becomes too burdensome and open, there might be
a chance to get back to the overall progress of mankind earlier. If the
powers that be are able to manage the system smoothly and skillfully, we
cannot make any prediction as to when the new dark ages will be over...But
for the rest of us, the only realistic option is to try to live in, with,
and from the world as bad as it has become. We need to build our own
communities nonetheless, virtual or real ones.
---
So one thing that we can do to help societies progress along is to provide
tools, knowledge and training for secure communications to every political
and social movement that shares at least some of our ideals.
Wrapping Up
If you alternate between writing Bash scripts and programs in non-shell programming languages, using the weak quotes and string appending strategies for long and short strings, respectively, may be a good approach. Otherwise, the alternative strategies may be preferable.
Regardless of your preferences, Bash gives you several great options to choose from.