How to store and echo multiple lines elegantly in bash? [duplicate] - linux

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 5 years ago.
I'm trying to capture a block of text into a variable, with newlines maintained, then echo it.
However, the newlines don't seemed to be maintained when I am either capturing the text or displaying it.
Any ideas regarding how I can accomplish this?
Example:
#!/bin/bash
read -d '' my_var <<"BLOCK"
this
is
a
test
BLOCK
echo $my_var
Output:
this is a test
Desired output:
this
is
a
test

You need to add " quotes around your variable.
echo "$my_var"

Related

Add markdown code tick to beginning and ending of a variable [duplicate]

This question already has an answer here:
How to escape backticks in bash
(1 answer)
Closed 3 months ago.
I searched all forums but didn't find a suitable way to achieve this.
variable
result=multiline
string and
other string
Want to convert it to
```result=multiline
string and
other string```
means add 3 ticks at the beginning and 3 ticks at the end of the file.
I know it's possible through sed, but I think it's not working because of special characters for example tick.
Any advice is appreciated.
Escaping backticks with \ helped.
echo $result > result.txt
echo "\`\`\`" | cat - result.txt > some-result.txt
echo "\`\`\`" >> markdown-result.txt

How to format linux mpstat output in multiple lines [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 3 years ago.
I have a small script where I appended the output of linux mpstat to a log file.
#/bin/bash
CPU_USAGE=$(mpstat)
echo $CPU_USAGE >> temp.log
The problem is that the output of mpstat on the terminal is formatted properly in 3 lines like so
However, the output to the file is all in one line.
How do I format the output like the one on the terminal?
Just quote the variable so it is not seen as several different parameters to be printed one after the other:
echo "$CPU_USAGE" >> temp.log
You could just directly pipe the output to the file:
#!/bin/bash
mpstat >> temp.log
If you must store it in a variable, then quote it like:
#!/bin/bash
CPU_USAGE=$(mpstat)
echo "$CPU_USAGE" >> temp.log
Otherwise, bash will not interpret the newlines as part of the message to echo, but the whole output as a list of short strings to output.

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

How to delete last found value in Bash [duplicate]

This question already has answers here:
Bash : extracting part of a string
(4 answers)
Closed 6 years ago.
Say I have a string 0.0.25, how do I delete the last part after dot (including it) to make it like 0.0? Note that last part can have variable number of digits.
In bash you can do the following:
echo "${var%.*}"
See the Shell Parameter Expansion section of the manual.
Using awk you could:
echo "0.0.25" | awk -F. '{print $1"."$2}'

BASH: sed to remove "/" from a $STRING [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 7 years ago.
First let me apologize for asking a question that:
Has some examples, though I find them confusing
Has a man page, also find confusing
Problem:
I would like to replace text in a $STRING within bash for a script I am writing. I chose to combine date/time to allow for easier end user integration.
STARTTIME="2015-03-17/11:30:00"
sed "Unknown"
Attempted Solution:
sed '/s// /' "$STARTTIME"
Desired result is to remove the "/" and end up with 2015-03-17 11:30:00 to then be passed to a command.
Thank you for any assistance.
If you're using bash, I would suggest that you used built-in string manipulation:
$ s='2015-03-17/11:30:00'
$ echo "${s/\// }"
2015-03-17 11:30:00
The syntax inside the braces means "replace the first occurrence of a forward slash (which needs escaping) with a space".

Resources