Just one file echoed from an array of filescp in multi line fashionparse one field from an JSON array into bash arrayBash Shell Script Array Length Off By OneArray from piped commands failscp from different directory to one directory certain filesbash script load an modify array from external file
How to use the passive form to say "This flower was watered."
Virtual destructor moves object out of rodata section
Will some rockets really collapse under their own weight?
Would getting a natural 20 with a penalty still count as a critical hit?
Do banks' profitability really suffer under low interest rates
Designing a prison for a telekinetic race
Why was ramjet fuel used as hydraulic fluid during Saturn V checkout?
My father gets angry everytime I pass Salam, that means I should stop saying Salam when he's around?
What's the point of writing that I know will never be used or read?
Tabularx with hline and overrightarrow vertical spacing
9 hrs long transit in DEL
Show two plots together: a two dimensional curve tangent to the maxima of a three dimensional plot
Reducing contention in thread-safe LruCache
Gofer work in exchange for Letter of Recommendation
What security risks does exposing the size of the plaintext entail?
Why did St. Jerome use "virago" in Gen. 2:23?
What is bodily formation? Does it refer to the breath or the body?
Just one file echoed from an array of files
What causes burn marks on the air handler in the attic?
Check disk usage of files returned with spaces
Can 'in-' mean both 'in' and 'no'?
Is there a way to make the "o" keypress of other-window <C-x><C-o> repeatable?
Why don't modern jet engines use forced exhaust mixing?
Can I check a small array of bools in one go?
Just one file echoed from an array of files
cp in multi line fashionparse one field from an JSON array into bash arrayBash Shell Script Array Length Off By OneArray from piped commands failscp from different directory to one directory certain filesbash script load an modify array from external file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I try to copy 2 or more files from one directory to another with cp
using an array.
I executed:
files=(
LocalSettings.php
robots.txt
.htaccess
$domain.png
googlec69e044fede13fdc.htm
)
filenames indented with tabulations;
I aim to execute afterwards:
cp -a "source_path/$files[@]" "/destanation_path"
my problem is that while testing the variable itself,echo $files
returned only the first filename LocalSettings.php
and not the full list of files.
How would you explain this?
Related: cp in multi line fashion;
bash ksh cp array
add a comment |
I try to copy 2 or more files from one directory to another with cp
using an array.
I executed:
files=(
LocalSettings.php
robots.txt
.htaccess
$domain.png
googlec69e044fede13fdc.htm
)
filenames indented with tabulations;
I aim to execute afterwards:
cp -a "source_path/$files[@]" "/destanation_path"
my problem is that while testing the variable itself,echo $files
returned only the first filename LocalSettings.php
and not the full list of files.
How would you explain this?
Related: cp in multi line fashion;
bash ksh cp array
Note how I, in my answer to the related question, usecd
to change to the source directory before copying. If I didn't do that, I would have to specify the full path to the files in the list, e.g.source_path/LocalSottings.php source_path/robots.txt
etc. Also$files
would always be the same as$files[0]
.
– Kusalananda♦
11 hours ago
add a comment |
I try to copy 2 or more files from one directory to another with cp
using an array.
I executed:
files=(
LocalSettings.php
robots.txt
.htaccess
$domain.png
googlec69e044fede13fdc.htm
)
filenames indented with tabulations;
I aim to execute afterwards:
cp -a "source_path/$files[@]" "/destanation_path"
my problem is that while testing the variable itself,echo $files
returned only the first filename LocalSettings.php
and not the full list of files.
How would you explain this?
Related: cp in multi line fashion;
bash ksh cp array
I try to copy 2 or more files from one directory to another with cp
using an array.
I executed:
files=(
LocalSettings.php
robots.txt
.htaccess
$domain.png
googlec69e044fede13fdc.htm
)
filenames indented with tabulations;
I aim to execute afterwards:
cp -a "source_path/$files[@]" "/destanation_path"
my problem is that while testing the variable itself,echo $files
returned only the first filename LocalSettings.php
and not the full list of files.
How would you explain this?
Related: cp in multi line fashion;
bash ksh cp array
bash ksh cp array
edited 7 hours ago
Gilles
570k136 gold badges1175 silver badges1687 bronze badges
570k136 gold badges1175 silver badges1687 bronze badges
asked 12 hours ago
JohnDoeaJohnDoea
241 gold badge11 silver badges46 bronze badges
241 gold badge11 silver badges46 bronze badges
Note how I, in my answer to the related question, usecd
to change to the source directory before copying. If I didn't do that, I would have to specify the full path to the files in the list, e.g.source_path/LocalSottings.php source_path/robots.txt
etc. Also$files
would always be the same as$files[0]
.
– Kusalananda♦
11 hours ago
add a comment |
Note how I, in my answer to the related question, usecd
to change to the source directory before copying. If I didn't do that, I would have to specify the full path to the files in the list, e.g.source_path/LocalSottings.php source_path/robots.txt
etc. Also$files
would always be the same as$files[0]
.
– Kusalananda♦
11 hours ago
Note how I, in my answer to the related question, use
cd
to change to the source directory before copying. If I didn't do that, I would have to specify the full path to the files in the list, e.g. source_path/LocalSottings.php source_path/robots.txt
etc. Also $files
would always be the same as $files[0]
.– Kusalananda♦
11 hours ago
Note how I, in my answer to the related question, use
cd
to change to the source directory before copying. If I didn't do that, I would have to specify the full path to the files in the list, e.g. source_path/LocalSottings.php source_path/robots.txt
etc. Also $files
would always be the same as $files[0]
.– Kusalananda♦
11 hours ago
add a comment |
4 Answers
4
active
oldest
votes
It's Bash
feature described in man bash
:
Referencing an array variable without a subscript is equivalent to
referencing the array with a subscript of 0.
If you want to print all members of files
array:
echo "$files[@]"
Also described in man bash
:
$name[@] expands each element of name to a separate word.
add a comment |
$files
and $files[0]
is equivalent when files
is a list such as the one you have in your question.
Note that "source_path/$files[@]"
only puts source_path/
before the first element of the list.
To modify the list in such a way that each element is prefixed by some path, you can do
files=( ... your list of files ... )
for element in "$files[@]"; do
files=( "$files[@]:1" "source_path/$element" )
done
cp "$files[@]" destanation_path
or, you could just cd
to source_path
before doing the cp
, or add the path to the actual names at the same time as you assign the values in the list from the start.
add a comment |
As others have pointed out, $files
only expands to the first element of the array, and "source_path/$files[@]"
only attaches "source_path/" to the first element. But there's a relatively simple way to get all elements, with a path prepended to each:
cp -a "$files[@]/#/source_path/" "/destanation_path"
This combines the all-elements expansion ([@]
) with a substitution. /#
means "replace at beginning of string", then the empty string to replace, then /
to delimit the replacement, then "source_path/" as the thing to replace (/add). This attaches the source path to each element, and doesn't get confused by funny characters in the elements like some versions do.
Note that in the above example, "source_path/" has a slash at the end but not the beginning; the "/" right before it is a delimiter. If it started with a slash and contained more slashes, like "/source/path/", it'd still work:
cp -a "$files[@]/#//source/path/" "/destanation_path"
add a comment |
You can also try the following snippet:
IFS=$'n'
cp -a $( printf "source_path/%sn" "$files[@]" ) /destination_path/
It should also work with filenames with spaces.
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, sincecp -a "$files[@]" /destination_path/
works strictly better.
– Gilles
7 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536125%2fjust-one-file-echoed-from-an-array-of-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's Bash
feature described in man bash
:
Referencing an array variable without a subscript is equivalent to
referencing the array with a subscript of 0.
If you want to print all members of files
array:
echo "$files[@]"
Also described in man bash
:
$name[@] expands each element of name to a separate word.
add a comment |
It's Bash
feature described in man bash
:
Referencing an array variable without a subscript is equivalent to
referencing the array with a subscript of 0.
If you want to print all members of files
array:
echo "$files[@]"
Also described in man bash
:
$name[@] expands each element of name to a separate word.
add a comment |
It's Bash
feature described in man bash
:
Referencing an array variable without a subscript is equivalent to
referencing the array with a subscript of 0.
If you want to print all members of files
array:
echo "$files[@]"
Also described in man bash
:
$name[@] expands each element of name to a separate word.
It's Bash
feature described in man bash
:
Referencing an array variable without a subscript is equivalent to
referencing the array with a subscript of 0.
If you want to print all members of files
array:
echo "$files[@]"
Also described in man bash
:
$name[@] expands each element of name to a separate word.
answered 11 hours ago
Arkadiusz DrabczykArkadiusz Drabczyk
8,9253 gold badges20 silver badges36 bronze badges
8,9253 gold badges20 silver badges36 bronze badges
add a comment |
add a comment |
$files
and $files[0]
is equivalent when files
is a list such as the one you have in your question.
Note that "source_path/$files[@]"
only puts source_path/
before the first element of the list.
To modify the list in such a way that each element is prefixed by some path, you can do
files=( ... your list of files ... )
for element in "$files[@]"; do
files=( "$files[@]:1" "source_path/$element" )
done
cp "$files[@]" destanation_path
or, you could just cd
to source_path
before doing the cp
, or add the path to the actual names at the same time as you assign the values in the list from the start.
add a comment |
$files
and $files[0]
is equivalent when files
is a list such as the one you have in your question.
Note that "source_path/$files[@]"
only puts source_path/
before the first element of the list.
To modify the list in such a way that each element is prefixed by some path, you can do
files=( ... your list of files ... )
for element in "$files[@]"; do
files=( "$files[@]:1" "source_path/$element" )
done
cp "$files[@]" destanation_path
or, you could just cd
to source_path
before doing the cp
, or add the path to the actual names at the same time as you assign the values in the list from the start.
add a comment |
$files
and $files[0]
is equivalent when files
is a list such as the one you have in your question.
Note that "source_path/$files[@]"
only puts source_path/
before the first element of the list.
To modify the list in such a way that each element is prefixed by some path, you can do
files=( ... your list of files ... )
for element in "$files[@]"; do
files=( "$files[@]:1" "source_path/$element" )
done
cp "$files[@]" destanation_path
or, you could just cd
to source_path
before doing the cp
, or add the path to the actual names at the same time as you assign the values in the list from the start.
$files
and $files[0]
is equivalent when files
is a list such as the one you have in your question.
Note that "source_path/$files[@]"
only puts source_path/
before the first element of the list.
To modify the list in such a way that each element is prefixed by some path, you can do
files=( ... your list of files ... )
for element in "$files[@]"; do
files=( "$files[@]:1" "source_path/$element" )
done
cp "$files[@]" destanation_path
or, you could just cd
to source_path
before doing the cp
, or add the path to the actual names at the same time as you assign the values in the list from the start.
edited 7 hours ago
answered 8 hours ago
Kusalananda♦Kusalananda
160k18 gold badges316 silver badges502 bronze badges
160k18 gold badges316 silver badges502 bronze badges
add a comment |
add a comment |
As others have pointed out, $files
only expands to the first element of the array, and "source_path/$files[@]"
only attaches "source_path/" to the first element. But there's a relatively simple way to get all elements, with a path prepended to each:
cp -a "$files[@]/#/source_path/" "/destanation_path"
This combines the all-elements expansion ([@]
) with a substitution. /#
means "replace at beginning of string", then the empty string to replace, then /
to delimit the replacement, then "source_path/" as the thing to replace (/add). This attaches the source path to each element, and doesn't get confused by funny characters in the elements like some versions do.
Note that in the above example, "source_path/" has a slash at the end but not the beginning; the "/" right before it is a delimiter. If it started with a slash and contained more slashes, like "/source/path/", it'd still work:
cp -a "$files[@]/#//source/path/" "/destanation_path"
add a comment |
As others have pointed out, $files
only expands to the first element of the array, and "source_path/$files[@]"
only attaches "source_path/" to the first element. But there's a relatively simple way to get all elements, with a path prepended to each:
cp -a "$files[@]/#/source_path/" "/destanation_path"
This combines the all-elements expansion ([@]
) with a substitution. /#
means "replace at beginning of string", then the empty string to replace, then /
to delimit the replacement, then "source_path/" as the thing to replace (/add). This attaches the source path to each element, and doesn't get confused by funny characters in the elements like some versions do.
Note that in the above example, "source_path/" has a slash at the end but not the beginning; the "/" right before it is a delimiter. If it started with a slash and contained more slashes, like "/source/path/", it'd still work:
cp -a "$files[@]/#//source/path/" "/destanation_path"
add a comment |
As others have pointed out, $files
only expands to the first element of the array, and "source_path/$files[@]"
only attaches "source_path/" to the first element. But there's a relatively simple way to get all elements, with a path prepended to each:
cp -a "$files[@]/#/source_path/" "/destanation_path"
This combines the all-elements expansion ([@]
) with a substitution. /#
means "replace at beginning of string", then the empty string to replace, then /
to delimit the replacement, then "source_path/" as the thing to replace (/add). This attaches the source path to each element, and doesn't get confused by funny characters in the elements like some versions do.
Note that in the above example, "source_path/" has a slash at the end but not the beginning; the "/" right before it is a delimiter. If it started with a slash and contained more slashes, like "/source/path/", it'd still work:
cp -a "$files[@]/#//source/path/" "/destanation_path"
As others have pointed out, $files
only expands to the first element of the array, and "source_path/$files[@]"
only attaches "source_path/" to the first element. But there's a relatively simple way to get all elements, with a path prepended to each:
cp -a "$files[@]/#/source_path/" "/destanation_path"
This combines the all-elements expansion ([@]
) with a substitution. /#
means "replace at beginning of string", then the empty string to replace, then /
to delimit the replacement, then "source_path/" as the thing to replace (/add). This attaches the source path to each element, and doesn't get confused by funny characters in the elements like some versions do.
Note that in the above example, "source_path/" has a slash at the end but not the beginning; the "/" right before it is a delimiter. If it started with a slash and contained more slashes, like "/source/path/", it'd still work:
cp -a "$files[@]/#//source/path/" "/destanation_path"
answered 7 hours ago
Gordon DavissonGordon Davisson
2,08512 silver badges10 bronze badges
2,08512 silver badges10 bronze badges
add a comment |
add a comment |
You can also try the following snippet:
IFS=$'n'
cp -a $( printf "source_path/%sn" "$files[@]" ) /destination_path/
It should also work with filenames with spaces.
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, sincecp -a "$files[@]" /destination_path/
works strictly better.
– Gilles
7 hours ago
add a comment |
You can also try the following snippet:
IFS=$'n'
cp -a $( printf "source_path/%sn" "$files[@]" ) /destination_path/
It should also work with filenames with spaces.
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, sincecp -a "$files[@]" /destination_path/
works strictly better.
– Gilles
7 hours ago
add a comment |
You can also try the following snippet:
IFS=$'n'
cp -a $( printf "source_path/%sn" "$files[@]" ) /destination_path/
It should also work with filenames with spaces.
You can also try the following snippet:
IFS=$'n'
cp -a $( printf "source_path/%sn" "$files[@]" ) /destination_path/
It should also work with filenames with spaces.
answered 10 hours ago
tinitatinita
1295 bronze badges
1295 bronze badges
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, sincecp -a "$files[@]" /destination_path/
works strictly better.
– Gilles
7 hours ago
add a comment |
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, sincecp -a "$files[@]" /destination_path/
works strictly better.
– Gilles
7 hours ago
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, since
cp -a "$files[@]" /destination_path/
works strictly better.– Gilles
7 hours ago
It works with file names containing spaces, but not with file names containing newlines or wildcards. It's also completely pointless, since
cp -a "$files[@]" /destination_path/
works strictly better.– Gilles
7 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536125%2fjust-one-file-echoed-from-an-array-of-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note how I, in my answer to the related question, use
cd
to change to the source directory before copying. If I didn't do that, I would have to specify the full path to the files in the list, e.g.source_path/LocalSottings.php source_path/robots.txt
etc. Also$files
would always be the same as$files[0]
.– Kusalananda♦
11 hours ago