3

In curl command, there is --data option, which substitutes the file contents when the option value is prefixed by @ symbol like,

curl -X POST --data @abc.json hostaddr.com

abc.json file contents are sent as the post request's body. This option is handy, if the post body is lengthy/multi-lined.

How can i do this in zshell or may be in, any linux shell? Is there a glob pattern/prefix operator, that will treat the following string as the file path and replace the string with the file contents?

1 Answers1

2

In most cases, that would be the redirection operator (<):

$ tr 'a' 'b' /path/to/file  ## fails because `tr` works on streams
tr: extra operand ‘file’
Try 'tr --help' for more information.
$ tr 'a' 'b' < /path/to/file ## works because the file's contents are passed to tr

Both command substitution and the redirection operator are defined by POSIX (see the link given above for command substitution) and should be available in just about any shell. Another relevant tool is command substitution. There are two ways of writing this, you can either enclose a command in backticks (`command`), or, you can write $(command). In general, the latter is to be preferred since it is a more powerful syntax that can deal with multiple, nested commands more cleanly.

So, to use a file's contents, you can write:

command $(cat /path/to/file)

or

command `cat /path/to/file`
terdon
  • 242,166