InsideDarkWeb.com

why printf executes string instead of formatting

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 "--------------"

Unix & Linux Asked on November 21, 2021

1 Answers

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!

Related Questions

Kali Linux 2020.2 Wifi Not Working (Realtek)

0  Asked on August 18, 2020 by harriet

     

Base64 encoded ignition file gets messed up

1  Asked on August 15, 2020 by michael-stbich

     

Weird textures on KDE Plasma 5.0

0  Asked on August 11, 2020 by rushisrinivas-k

     

How do I syntax check a Zsh script?

1  Asked on August 9, 2020 by akhil-jalagam

     

How can I swap my two screens, left to right?

5  Asked on August 4, 2020 by ripper234

         

Adding suffix to filename during for loop in bash

1  Asked on August 3, 2020 by mishal-ahmed

       

Why are aliases skipped if escaped?

2  Asked on August 2, 2020

     

Why can’t I type a g̃ the same way I type ñ?

1  Asked on July 29, 2020 by mmaluff

         

Custom logrotate with hostnames

0  Asked on July 29, 2020 by gwynn

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.