I have a shell function (in .bashrc) which creates a temp file, executes the arguments (including all sequence of pipelines), redirects it to a temp file and then open it in VS Code.
I invoke the shell function as
Temp "ls | nl"
In the follwoing code, I tried to make it work. I break the entire string with IFS as a space, store it in an array. Now I want to execute the entire sequence of pipelines and redirect the final stdout to the temp file. How can I achieve this.? I know that for loop won't work here. But I want to print the entire array.
Temp() {
a="$(mktemp --suffix=.md "/tmp/$1-$(GetFilename "$2")-XXX")"
IFS=' ' read -r -a Array <<< "$1"
nArray=${#Array[@]} # Size of array
for ((i=0; i<nArray; i=i+1)); do
done
#"${@}" > "$a"
code -r "$a"
}
In the interactive terminal session the following works:
cat <<EOF
$(ls | nl)
EOF
So, I tried Heredoc, instead of the for loop inside the function
cat > "$a" <<EOF
$($1)
EOF
But this puts quotes around | and thus fails.
The above would work, if we could remove the quotes somehow.
This also doesn't work
cat > "$a" <<EOF
$(echo $1 | sed "s/'//g")
EOF