Linked Questions
15 questions linked to/from Trying to split a string into two variables
2
votes
3
answers
1k
views
Why is my list being stripped of commas? [duplicate]
I'm trying to make a menu function, and have the following script segment, which is meant to split a comma separated list of values into an array:
IFS=,
read -r -a optionList <<< $3
However, ...
2
votes
1
answer
5k
views
bash IFS split string into array [duplicate]
I have the following line filename:231:blahblah that I want to split into an array using : as the delimiter
The is the code that I have
echo "Text read from file: $line"
IFS=':' read -a FILENAME <...
1
vote
1
answer
2k
views
Bash/shell script: syntax error in expression [duplicate]
I'm new shell scripting and have a quick question about my code.
This works:
x=2
y=4
z=6
x=$(( $x-1 ))
y=$(( $y-2 ))
z=$(( $z-3 ))
echo $x $y $z
$ script.sh
1 2 3
And this works:
n=2,4,6
IFS=$',' ...
4
votes
2
answers
123
views
Parsing a delimited string into an array in Bash - why is "$var" different from $var though $var has no spaces? [duplicate]
I am running Bash version 4.2.25. Here is my code:
#!/usr/bin/env bash
string="one:two:three:four"
# without quotes
IFS=: read -ra array_1 <<< $string
for i in "${array_1[@]}"; do printf "...
1
vote
1
answer
84
views
Why does "read" here incorrectly populates array when used with unquoted here-string? [duplicate]
I have encountered a strange behavior when using the read command and unquoted here-string when they still used to be subject to word-splitting (in older versions of bash). Please take a look at the ...
3030
votes
40
answers
3.9m
views
How do I split a string on a delimiter in Bash?
I have this string stored in a variable:
IN="[email protected];[email protected]"
Now I would like to split the strings by ; delimiter so that I have:
ADDR1="[email protected]"
ADDR2="[email protected]"
I don't ...
7
votes
3
answers
10k
views
Bash: How to split a string and assign multiple variables
I have to split a URL string and assign variables to few of the splits. Here is the string
http://web.com/sub1/sub2/sub3/sub4/sub5/sub6
I want to assign variables like below from bash
var1=sub2
var2=...
1
vote
2
answers
1k
views
How to read an array from a variable split using IFS
I am trying to initialize read an array by splitting a variable.
x=abc:def:gh
declare -a xa
# I want xa[0] = abc, xa[1] = def, and so on
IFS=: read -a xa <<< $x
echo ${#xa[@]} $xa ######### ...
1
vote
2
answers
4k
views
Options menu with array
I am trying to make a script to get ip list from file and show it on screen with select option, and make ssh to that IP just by selecting. File is like below;
name1 1.1.1.1
name2 2.2.2.2
name3 3.3.3....
0
votes
1
answer
556
views
IFS change with Bash 4.2
Running these commands gives expected results
$ bash --version
GNU bash, version 4.1.11(2)-release
$ foo=(111 222 333)
$ IFS=, cat <<< "${foo[*]}"
111,222,333
However it appears with Bash ...
0
votes
2
answers
355
views
How can I split a string in KSH bash script?
Here's a link
I referred this but I am not able to understand, how I can split:
Testing123 into Testing and 123
temp=${str%%:*} #only if there was colon in between but in my case there isn't.
If ...
3
votes
1
answer
157
views
Bash IFS, getting zapped in function call
Platform CentOS Linux release 7.6.1810, working in bash.
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
This is an idiom I've seen recommended for parsing text in bash in general and ...
0
votes
1
answer
94
views
Bash syntax error when comparing a value with an array
I have the following arrays defined in a bash script
IFS=, read -r -a start_files <<< $(head -n 1 file.txt)
IFS=, read -r -a end_files <<< $(tail -n 1 file.txt)
I read also two ...
0
votes
1
answer
101
views
why does it not work while overwrite IFS and read multi-data info variables?
I want to read result of ps command and the proc number into two variables, but all the output assigned to the first variable.
my shell followed like this
#!/bin/bash
function status() {
proc_num=...
1
vote
1
answer
79
views
bash - have read respect newlines (i.e. \n) contained on a string
I have a function that returns several string separated by "|" and i am using read to parse them in tokens. Some of the strings might contain newlines (i.e. \n) but from my perspective read ...