Intro
I am writing a bash script that is being executed from Jenkins Pipeline. Groovy DSL is not a problem for me, I am using bare shell commands.
For every command so far I have been using bash -c
and passed over commands I wanted, like below:
ssh -l <user> <host> bash -c "'
cd /my/path
./script.sh param1 param2
echo $MY_VAR
'"
That works great.
The problem
Now I had a more complicated command:
ls -1t | egrep 'regexp' | tail -n +10 | xargs rm -rf
Basically listing some directory and deleting all items but for last 10. It works using in bash
but it does not with bash -c
.
I also tried a for-loop version:
for f in $(ls -1t | egrep 'regexp' | tail -n +10); do rm -rf $f; done;
But it does not work either. I get an error, like:
bash: line 4:
for f in <actual_item1> <actual_item2> <actual_item3>; do echo <actual_item1>; done;
No such file or directory
So there is some issue with $
variable resolution. How to overcome this?
Or how do I make the piped version work?