Questions tagged [ifs]

IFS is a variable in Unix shells (Bourne, POSIX sh, bash, ksh, …) that controls how unescaped substitutions are split into words.

IFS (short for "input field separator" and often referred to as "internal field separator") is a variable in Unix shells (, , , , , , , , …) that controls how strings are split into multiple fields. Most notably, variable substitutions (e.g. $foo) which are not inside double quotes or in a few other protected locations are split into multiple fields based on the value of IFS; the same goes for command substitutions (e.g. $(foo)). Each character that is present in IFS is a field separator. The default value contains a space, a tab and a newline.

For other meanings of the acronym IFS, use the appropriate tag:

  • IBM Integrated file system
  • QNX Image Filesystem
  • ERP by Industrial and Financial Systems AB
293 questions
3
votes
1 answer

SC2207 Bash array assignment from subshell not splitting as expected

I have been populating an array using: AWS_STS_CREDS=( $(aws sts ...) ) This raises shellcheck error SC2207 Prefer mapfile or read -a to split command output But the recommendation does not work as expected. IFS=" " read -r -a AWS_STS_CREDS <<<…
beanaroo
  • 506
  • 1
  • 5
  • 14
3
votes
1 answer

Why does read throw an error in bash but works fine?

This bash script writes an array to a file and then reads the file back into a different array. (This is useful for array-based communication between scripts.) However, a strange, non-reported error is trapped by the IFS line (line 12).…
DarkerIvy
  • 1,477
  • 14
  • 26
3
votes
1 answer

How to prevent word splitting while using eval to run a command stored in $@?

I am trying to use eval to run a command passed into a function through $@. Here is my code: run_command() { : some logic not relevant to this question eval "$@" } I am running it as: run_command "ls" "|" "wc -l" # works, runs "ls | wc…
learningbee
  • 333
  • 1
  • 5
  • 11
3
votes
2 answers

splitting a string into individual characters in bash using IFS

I'm trying to split a string into individual characters. For example temp="hello" into "h", "e", "l", "l", "o" I tried using IFS because that's what I used in previous string splits and wanted to keep the consistency across the script. IFS='' read h…
Krin123
  • 323
  • 1
  • 9
  • 17
3
votes
3 answers

Bash IFS ('\n'): Problems with file name detection through find command and for loop

#!/bin/bash IFS='\n' declare -i count=0 AX=$(find *.iso -maxdepth 1 -type f) # Rather use AX="$(find *.iso -maxdepth 1 -type f"? # A="${AX%x}" < Could I use this when applying "" to $() in AX? But it should already include newlines like this way.…
3
votes
1 answer

How to properly process and print files with spaces in bash

I'm writing a simple recursive ls program in bash (which I'm very not experienced at, so feel free to be brutal). The program is supposed to print out each file (possibly directory) on a separate line, and each time a new directory is entered, the…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
3
votes
2 answers

How to view git log using column unit separators?

How can I get git log --graph to print using column unit separators? I would like the messages to line up vertically, rather than being indented by the graph. Example command to print the branch graph, author name, and message: git log --graph…
joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
3
votes
2 answers

Why does my string not split nicely into an array?

I have that bash script that shall read the output of git show into an array. First, I create a string with the following statement: git show --quiet --date=rfc --pretty=format:"%s|%b|%an, %ae|%H|%cd" HEAD Which gives me the pattern that I…
eckes
  • 64,417
  • 29
  • 168
  • 201
3
votes
5 answers

BASH: How do you "split" the date command?

Cygwin user here (though if there's a suitable solution I will carry it over to K/Ubuntu, which I also use). I have a Welcome message in my .bashrc that looks like the following: SAD=(`date +%A-%B-%d-%Y`) DUB=(`date -u +%k:%M`) printf "Today's Date…
silversleevesx
  • 75
  • 1
  • 2
  • 5
3
votes
2 answers

Bash: Using IFS to join an array into a string

Can someone explain what's going on here. All of this is executed consecutively in the same shell. $ array=(a b c) $ echo "${array[*]}" a b c So far so good. $ IFS=/ echo "${array[*]}" a b c That's ok, the expansion happened before the whole line…
ultracrepidarian
  • 1,080
  • 11
  • 18
3
votes
4 answers

How bash IFS is working in this example?

I am reading about bash internal variables and came across this IFS example: output_args_one_per_line() { for arg do echo "[$arg]" done # ^ ^ Embed within brackets, for your viewing pleasure. } CASE1 IFS=" " var=" a b c …
RanRag
  • 48,359
  • 38
  • 114
  • 167
3
votes
2 answers

C: IFS System() Vulnerability

For educational reasons I have to exploit an C-Code The Programm set the egid first, and then the vulnerability with the system("/usr/bin/..."); Command. So I made an 'usr' executeable in my Home-Directory and set the Path to the Home…
nikmaster
  • 421
  • 1
  • 6
  • 19
2
votes
2 answers

why there is different output in for-loop

Linux bash: why the two shell script as follow had different result? [root@yumserver ~]# data="a,b,c";IFS=",";for i in $data;do echo $i;done a b c [root@yumserver ~]# IFS=",";for i in a,b,c;do echo $i;done a b c expect output:…
peaqe
  • 23
  • 3
2
votes
0 answers

How can I use IFS for seperator to join a subset of an array in Bash's here document?

I have an array, and I would like to concatenate a subset of it, passing it along with some text as stdin for other commands to use. This works in here string but not in here document, even the same code: declare -a arr=(12 34 56 78 90) IFS=',' #…
wuxunfeng666
  • 39
  • 1
  • 4
2
votes
2 answers

IFS more than a character usage with R script and bash

I am using bash scripting to write and run R scripts: since I was not proficient in R I used to write loops and conditions in the bash script that then were translated in the R scripts or "here documents"... as you can imagine the R_scripts produced…
Mareczek
  • 304
  • 1
  • 2
  • 13
1 2
3
19 20