0

I'm trying to create a filename using command param as such but not sure how to go about doing it. This is what I was trying:

echo "AZ" |xargs date >> $1.txt

I'm trying to create a file named AZ.txt with the date in it.

codeBarer
  • 2,238
  • 7
  • 44
  • 75
  • Are you trying to write the date to a file named `AZ.txt`, or `AZ` to a file named `2020-06-17.txt`, or something else? – chepner Jun 17 '20 at 20:15
  • I'm trying to write filename AZ.txt – codeBarer Jun 17 '20 at 20:24
  • 1
    Is `date >> AZ.txt` what you are looking for? I didn't understand where the parameter whould be used – Leonardo Dagnino Jun 17 '20 at 20:35
  • To be clear, with the code here, the `>> $1.txt` happens **before** `xargs` is started. It's not an argument to `xargs`, it's an instruction to the shell that runs xargs to redirect the output of `xargs` somewhere. `xargs` never sees it and can't change it. – Charles Duffy Jun 17 '20 at 20:52

2 Answers2

2

If you must use xargs, then you'll have to wrap the rest of it in a shell script to delay execution of the redirection until you have constructed the filename:

echo AZ | xargs sh -c 'date >> "$1".txt' sh

The trailing "sh" is to force the xargs argument "AZ" into $1 instead of $0

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

If the name of the file comes from some command, you can use command substitution:

date > "$(echo AZ)".txt
chepner
  • 497,756
  • 71
  • 530
  • 681