TransWikia.com

why printf executes string instead of formatting

Unix & Linux Asked on November 21, 2021

I’m newbie in bash scripting

I want to format command with printf to prepare execution and assign into variable.

But it is executing immediately. It is listing /opt/cinar/packages/libcnrhttp2_*
instead of writing it as string into command string.

I was expecting this:

ssh [email protected] ls /opt/cinar/packages/libcnrhttp2_* -lr | awk 'NR==1'

enter image description here

#!/bin/bash
declare -r libcnrhttp2="libcnrhttp2_"
declare -r root="/opt/cinar/packages"
declare -r destination="/opt/cinar/packages"
declare -r id=ctopkaya
declare -r host=192.168.13.137
declare -r remote=$id"@"$host

declare cmd=""
printf -v cmd ssh %s ls %s/%s* -lr | awk 'NR==1' $remote /opt/cinar/packages libcnrhttp2_

cd $destination
echo "--------------"
echo $cmd
echo "--------------"

One Answer

The command which is supposed to format the text is within double quotes and is thus treated as text to print. To print the result of a command instead put it inside $() (command substitution), like:

printf "$(printf foo | awk 'NR==1') bar"

Answered by DisplayName on November 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP