You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash中路径添加变量名异常:如何生成正确命名的输出文件

Fixing the Empty Variable Issue in Your Filename

Alright, let's break down why you're getting merged_.bam instead of the expected merged_E2_ER.bam—it all boils down to how you're assigning the sample_name variable.

The Root Cause

When you wrote:

sample_name=${E2_ER}

You're telling the shell to pull the value of an existing variable named E2_ER and assign it to sample_name. If you never defined a variable called E2_ER before this line, sample_name ends up being an empty string. That's why the filename loses the sample name part entirely.

The Simple Fix

If you want sample_name to be the literal string E2_ER (not the value of another variable), just assign it directly without the curly braces and dollar sign:

sample_name=E2_ER

Updated Full Code

Here's how your variables should look now (I added the .bam suffix directly to output_file since your result mentions it):

sample_name=E2_ER
files_source=/home/datase/reads/E2_ER/output_files
output_file=${files_source}/merged_${sample_name}.bam

Now when you run your merge script, the output file will correctly be merged_E2_ER.bam.

Bonus: If You Did Mean to Use a Pre-Defined Variable

If you actually intended E2_ER to be a pre-set variable with a custom value, make sure you define it first before assigning sample_name:

# First define the E2_ER variable with your desired value
E2_ER="my_custom_sample"
sample_name=${E2_ER}
# Then the rest of your code
files_source=/home/datase/reads/E2_ER/output_files
output_file=${files_source}/merged_${sample_name}.bam

This would generate merged_my_custom_sample.bam instead.

内容的提问来源于stack exchange,提问作者Bella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:12:11