2
line="\t\t\t1\t2\t\t3" 

What I do:

IFS=$'\t' DIRS=($line); 

What I want to get:

DIRS[0]=NULL; DIRS[1]=NULL; DIRS[2]=NULL; DIRS[3]=1;DIRS[4]=2;DIRS[5]=NULL;DIRS[6]=3; 

What I actually get:

DIRS[0]=1; DIRS[1]=2; DIRS[2]=3 

Is that possible to get what I want to get?

0

1 Answer 1

6

Bash treats whitespace specially in IFS:

If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter.

So, use a non-whitespace character. (BTW, I use $'\t' for a tab, search man bash for Quoting)

#!/bin/bash line=$'\t\t\t1\t2\t\t3' IFS=$':' DIRS=(${line//$'\t'/:}) # Replace tabs with colons. for (( i=0 ; i<${#DIRS[@]} ; i++ )); do echo "$i: [${DIRS[i]}]" done 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it works for them. But instead of using ':' as delimiter I use '|'.
@TrueBlue10: Use whatever can't occur in the $line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.