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
Related
This question already has answers here:
is it safe to use "ls" in for loop in bash
(3 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have many files with pattern name:
N.apple(d).log
e.g. 001.apple(d).log, 002.apple(d).log, etc.
The problem is when I am trying to do something with the files in a bash script, there is always an error 'No such file or directory' even with escaped brackets:
for i in $(ls my_folder); do file=$(echo $i | sed 's/(/\\(/g' | sed 's/)/\\)/g') ; head -1 my_folder/$file; done
Thanks for any tips
Use quotes. Also, don't use ls when the shell can expand the wildcards just fine already.
for i in my_folder/*; do head -1 "$i"; done
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!
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}'
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".
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"