Во-первых, я не верю тебе. ls ../images
будет выводить только базовые имена, например:
a.png
b.png
...etc...
A разные команда ls ../images/*
может дать вывод, который вы показываете.
Хотя в системе, использующей ls
из GNU coreutils, например, в Linux, если выводом является терминал и , имена файлов короткие, как вы показали, ls
без параметров подойдетвывод в виде нескольких столбцов больше похож на:
../images/aaaaa.png ../images/ddddd.png ../images/ggggg.png
../images/b.png ../images/eeeee.png ../images/hhhhhh.png
../images/ccccc.png ../images/f.png ../images/iiiiii.png
, если у вас нет псевдонима (или функции или сценария теневого копирования), который заставляет опцию -1
(one) предотвратить это. Или фактические имена (значительно) длиннее, как они, вероятно, должны быть. Или вы что-то пускаете, даже cat
, потому что тогда вывод ls
- это труба, а не терминал. Эти детали имеют значение.
В некоторых случаях sed так же хорош, как awk, и обычно более краткий:
ls | sed 's/^/![](/; s/$/)/'
# sed processes a string of command(s) for each line read from stdin,
# and writes the result to stdout (by default, -n overrides this)
# s/old/new/ is string replacement using regex (which can be complex)
# ^ in old matches the beginning of the line
# $ in old matches the end of the line
# this assumes none of your filenames contains a newline character
# which Unix permits, but is rarely done because it is quite confusing
или , так как вам нравится printf может выполнять всю работу:
printf '![](%s)\n' ../images/*
# printf takes a format string containing any mixture of literal text
# (including backslash+letter escapes mostly the same as C et succ)
# and conversion specifiers beginning with % which each interpolate one argument
# it repeats the format as needed to handle all the arguments, so here
# with one c.s. it repeats the format once for each argument = filename
# this 'works' even if a filename contains newline
# (but I don't think the result will work correctly as markdown)