Sunday, March 13, 2022

[SOLVED] Modify shell variables using sed with POSIX compliance

Issue

I have this shell variable:

#!/bin/sh
namelist="John Doe
Josh Franklin
John Adams
"

To delete the lines containing "John", I use:

namelist=$(sed '/John/d' <<< "$namelist")

However, after checking for POSIX compatibility from the website called Shellcheck,

it says that

In POSIX sh, here-strings are undefined.

So what would be the POSIX compliant way of modifying a variable using sed?


Solution

what would be the POSIX compliant way of modifying a variable using sed?

Just don't use <<< and just output it to sed.

 namelist=$(printf "%s\n" "$namelist" | sed '/John/d')


Answered By - KamilCuk
Answer Checked By - Terry (WPSolving Volunteer)