0

So I have this command: ls /opt/logs/rsyncTesting0/ | awk '{print $0}' | cut -d '-' -f6 | cut -d '.' -f1

But I can't seem to save it in a bash variable because every time I try to echo it, I get a blank string. Any ideas on why this is, I'm fairly new to bash.

currentFileHour=ls /opt/logs/rsyncTesting0/ | awk '{print $0}' | cut -d '-' -f6 | cut -d '.' -f1
norok2
  • 25,683
  • 4
  • 73
  • 99
noobcoderiam
  • 488
  • 4
  • 19

1 Answers1

0

To capture the output of a command, such as your pipeline there, enclose it either in backticks, or, preferred, in $( ... ).

currentFileHour=$(ls /opt/logs/rsyncTesting0/ | awk '{print $0}' | cut -d '-' -f6 | cut -d '.' -f1)
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • solid buddy thanks. If i want to save it as an array is it like this - ($(command)) – noobcoderiam Dec 03 '19 at 22:37
  • 1
    @noobcoderiam, that's actually *not* a good practice for writing to arrays. See [BashPitfalls #50](http://mywiki.wooledge.org/BashPitfalls#hosts.3D.28_.24.28aws_....29_.29). – Charles Duffy Dec 03 '19 at 22:39
  • 2
    @Tanktalus, obvious duplicates should be closed, not answered, so people get answers that have already been vetted/refined/&c; writing a new answer every time a common question gets re-asked means we have a multitude of competing answers, rather than settling on a "best one" that gets as many eyeballs (and as much competition and editing) as possible. – Charles Duffy Dec 03 '19 at 22:40
  • @CharlesDuffy thats true Charles, should I close this off? – noobcoderiam Dec 03 '19 at 22:59
  • @noobcoderiam, so, the general rule for whether duplicates should be deleted is that they should be kept if they use search terms that (1) someone else with the same problem is likely to use, and (2) that don't already find the duplicate or an already-answered question linked to it. If this question is likely to show up in someone's search results where the original (or a different, preexisting duplicate) wouldn't, then it's worth keeping around. (That's why we let questions flagged as duplicates be upvoted). – Charles Duffy Dec 04 '19 at 16:41
  • @CharlesDuffy I think I'll leave it up because I now sometimes 2 articles may have the same answer, but the way 1 guy explains it may click with someone than the way the other guy explains it, if that makes sense – noobcoderiam Dec 07 '19 at 19:15
  • @noobcoderiam, keeping duplicates around should be about the *questions*, not the answers -- if the canonical instance of a question needs a different/better answer, it can and should be added there so as many people see it as possible. – Charles Duffy Dec 09 '19 at 14:34