Skip to content

Commit

Permalink
fix: honor IFS in read builtin (#208)
Browse files Browse the repository at this point in the history
Ensure splitting in 'read' honors the current effective value of IFS in the current environment, rather than always split by ASCII whitespace.
  • Loading branch information
reubeno authored Oct 16, 2024
1 parent 6cf65ed commit 552eec8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 11 additions & 4 deletions brush-core/src/builtins/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ impl builtins::Command for ReadCommand {
let input_line = self.read_line(input_stream, context.stdout())?;

if let Some(input_line) = input_line {
let mut fields: VecDeque<_> = input_line
.split_ascii_whitespace()
.map(|field| field.to_owned())
.collect();
let mut fields: VecDeque<_> = split_line_by_ifs(&context, input_line.as_str());

// If -a was specified, then place the fields as elements into the array.
if let Some(array_variable) = &self.array_variable {
Expand Down Expand Up @@ -274,3 +271,13 @@ impl ReadCommand {
Ok(orig_term_attr)
}
}

fn split_line_by_ifs(context: &commands::ExecutionContext<'_>, line: &str) -> VecDeque<String> {
// Retrieve effective value of IFS for splitting.
let ifs = context.shell.get_ifs();
let split_chars: Vec<char> = ifs.chars().collect();

line.split(split_chars.as_slice())
.map(|field| field.to_owned())
.collect()
}
7 changes: 7 additions & 0 deletions brush-shell/tests/cases/builtins/read.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ cases:
stdin: |
read myvar < <(echo hello)
echo "myvar: ${myvar}"
- name: "read with custom IFS"
stdin: |
content=" a b c "
while IFS= read line; do
echo "LINE: '$line'"
done <<<"${content}"

0 comments on commit 552eec8

Please sign in to comment.